home *** CD-ROM | disk | FTP | other *** search
/ Australian Personal Computer 2002 November / CD 1 / APC0211D1.ISO / workshop / prog / files / ActivePerl-5.6.1.633-MSWin32.msi / _74176b65d6964e69c8deffc62f77b0b8 < prev    next >
Encoding:
Text File  |  2002-06-17  |  140.6 KB  |  4,844 lines

  1. @rem = '--*-Perl-*--
  2. @echo off
  3. if "%OS%" == "Windows_NT" goto WinNT
  4. perl -x -S "%0" %1 %2 %3 %4 %5 %6 %7 %8 %9
  5. goto endofperl
  6. :WinNT
  7. perl -x -S %0 %*
  8. if NOT "%COMSPEC%" == "%SystemRoot%\system32\cmd.exe" goto endofperl
  9. if %errorlevel% == 9009 echo You do not have Perl in your PATH.
  10. if errorlevel 1 goto script_failed_so_exit_with_non_zero_val 2>nul
  11. goto endofperl
  12. @rem ';
  13. #!perl 
  14. #line 15
  15.  
  16. require 5.006;    # require 5.6.0
  17. use strict;
  18.  
  19. # A command-line shell implementation. The code which invokes it is at the
  20. # bottom of this file.
  21. package PPMShell;
  22. use base qw(PPM::Term::Shell);
  23.  
  24. use Data::Dumper;
  25. use Text::Autoformat qw(autoformat form);
  26. use Getopt::Long;
  27.  
  28. # These must come _after_ the options parsing.
  29. require PPM::UI;
  30. require PPM::Trace;
  31. PPM::Trace->import(qw(trace));
  32.  
  33. my $NAME    = q{PPM - Programmer's Package Manager};
  34. my $SHORT_NAME    = q{PPM};
  35. my $VERSION    = '3.0.1';
  36.  
  37. sub dictsort(@);
  38.  
  39. #=============================================================================
  40. # Output Methods
  41. #
  42. # PPM behaves differently under different calling circumstances. Here are the
  43. # various classes of messages it prints out:
  44. # 1. error/warning    - an error or "bad thing" has occurred
  45. # 2. informational    - required information like search results
  46. # 3. verbose        - verbose that's only needed in interactive mode
  47. #
  48. # Here are the cases:
  49. # 1. PPM is in interactive mode: everything gets printed.
  50. # 2. PPM is in batch mode: everything minus 'verbose' gets printed.
  51. #=============================================================================
  52. sub error {
  53.     my $o = shift;
  54.     return 1 unless $o->{SHELL}{output}{error};
  55.     CORE::print STDERR @_;
  56. }
  57. sub errorf {
  58.     my $o = shift;
  59.     return 1 unless $o->{SHELL}{output}{error};
  60.     CORE::printf STDERR @_;
  61. }
  62. sub warn { goto &error }
  63. sub warnf { goto &errorf }
  64. sub inform {
  65.     my $o = shift;
  66.     return 1 unless $o->{SHELL}{output}{inform};
  67.     CORE::print @_;
  68. }
  69. sub informf {
  70.     my $o = shift;
  71.     return 1 unless $o->{SHELL}{output}{inform};
  72.     CORE::printf @_;
  73. }
  74. sub verbose {
  75.     my $o = shift;
  76.     return 1 unless $o->{SHELL}{output}{verbose};
  77.     CORE::print @_;
  78. }
  79. sub verbosef {
  80.     my $o = shift;
  81.     return 1 unless $o->{SHELL}{output}{verbose};
  82.     CORE::printf @_;
  83. }
  84. sub assertw {
  85.     my $o = shift;
  86.     my $cond = shift;
  87.     my $msg = shift;
  88.     $o->warn("Warning: $msg\n") unless $cond;
  89.     return $cond;
  90. }
  91. sub assert {
  92.     my $o = shift;
  93.     my $cond = shift;
  94.     my $msg = shift;
  95.     $o->error("Error: $msg\n") unless $cond;
  96.     return $cond;
  97. }
  98.  
  99. sub mode {
  100.     my $o = shift;
  101.     $o->{SHELL}{mode};
  102. }
  103. sub setmode {
  104.     my $o = shift;
  105.     my $newmode = shift || '';
  106.     my $oldmode = $o->{SHELL}{mode};
  107.     if ($newmode eq 'SHELL') {
  108.     $o->{SHELL}{output}{error}   = 1;
  109.     $o->{SHELL}{output}{inform}  = 1;
  110.     $o->{SHELL}{output}{verbose} = 1;
  111.     }
  112.     elsif ($newmode eq 'BATCH') {
  113.     $o->{SHELL}{output}{error}   = 1;
  114.     $o->{SHELL}{output}{inform}  = 1;
  115.     $o->{SHELL}{output}{verbose} = 0;
  116.     }
  117.     elsif ($newmode eq 'SCRIPT') {
  118.     $o->{SHELL}{output}{error}   = 1;
  119.     $o->{SHELL}{output}{inform}  = 1;
  120.     $o->{SHELL}{output}{verbose} = 0;
  121.     }
  122.     elsif ($newmode eq 'SILENT') {
  123.     $o->{SHELL}{output}{error}   = 1;
  124.     $o->{SHELL}{output}{inform}  = 0;
  125.     $o->{SHELL}{output}{verbose} = 0;
  126.     }
  127.     $o->{SHELL}{mode} = $newmode;
  128.     return $oldmode;
  129. }
  130.  
  131. # Older versions of PPM3 had one "Active" repository. This code reads
  132. # $o->conf('repository') if it exists, and moves it into
  133. # $o->conf('active_reps'), which is a list. The old one is deleted -- old PPMs
  134. # will reset it if needed, but it will be ignored if 'active_reps' exists.
  135. sub init_active_reps {
  136.     my $o = shift;
  137.  
  138.     if ($o->conf('repository') and not $o->conf('active_reps')) {
  139.     my @active = $o->conf('repository');
  140.     delete $o->{SHELL}{conf}{DATA}{repository};
  141.     $o->conf('active_reps', \@active);
  142.     }
  143.     elsif (not defined $o->conf('active_reps')) {
  144.     my @active = $o->reps_all; # enable all repositories
  145.     $o->conf('active_reps', \@active);
  146.     }
  147. }
  148.  
  149. sub init {
  150.     my $o = shift;
  151.     $o->cache_clear('query');
  152.     $o->cache_clear('search');
  153.     $o->{API}{case_ignore} = 1;
  154.  
  155.     # Load the configuration;
  156.     $o->{SHELL}{conf} = PPM::Config::load_config_file('cmdline');
  157.     $o->init_active_reps;
  158.  
  159.     # check whether there's a target in the parent's perl that hasn't been
  160.     # installed in the "targets" file:
  161.     my $ppmsitelib = $ENV{PPM3_PERL_SITELIB};
  162.     if ($ppmsitelib and opendir(PPMDIR, "$ppmsitelib/ppm-conf")) {
  163.         my @files = map  { "$ppmsitelib/ppm-conf/$_" }
  164.                 grep { /^ppminst/i && !/(~|\.bak)\z/ } readdir PPMDIR;
  165.     closedir PPMDIR;
  166.     my $found = 0;
  167.     if (@files == 1) {
  168.         my @targets = PPM::UI::target_list()->result_l;
  169.         for my $target (@targets) {
  170.         my $info = PPM::UI::target_raw_info($target);
  171.         next unless $info and $info->is_success;
  172.         ++$found and last
  173.             if path_under($info->result->{path}, $files[0]);
  174.         }
  175.         unless ($found) {
  176.         # We're going to add a new target:
  177.         # 1. if we can find ppm3-bin.cfg, use that
  178.         # 2. if not, guess lots of stuff
  179.         my $ppm3_bin_cfg = "$ENV{PPM3_PERL_PREFIX}/bin/ppm3-bin.cfg";
  180.         my $r = PPM::UI::target_add(undef, From => $ppm3_bin_cfg)
  181.             if -f $ppm3_bin_cfg;
  182.         unless ($r and $r->is_success) {
  183.             PPM::UI::target_add(
  184.             'TEMP',
  185.             type => 'Local',
  186.             path => $files[0],
  187.             );
  188.         }
  189.         }
  190.     }
  191.     }
  192.  
  193.     # set the initial target:
  194.     if (defined $o->{API}{args}{target}) {
  195.     my $t = $o->{API}{args}{target};
  196.     my $prefix = $ENV{PPM3_PERL_PREFIX};
  197.     if ($t ne 'auto') {
  198.         # A full name or number given:
  199.         $o->run('target', 'select', $o->{API}{args}{target});
  200.     }
  201.     elsif ($prefix) {
  202.         # Auto-select target, based on where we came from:
  203.         my @l = $o->conf('target');
  204.         push @l, PPM::UI::target_list()->result_l;
  205.         for my $target (@l) {
  206.         next unless $target;
  207.         my $info = PPM::UI::target_raw_info($target);
  208.         next unless $info and $info->is_success;
  209.         next unless path_under($info->result->{path}, "$prefix/");
  210.         my $mode = $o->setmode('SILENT');
  211.         $o->run('target', 'select', $target);
  212.         $o->setmode($mode);
  213.         last;
  214.         }
  215.     }
  216.     }
  217. }
  218.  
  219. sub preloop {
  220.     my $o = shift;
  221.  
  222.     if ($o->conf('verbose-startup') and $o->mode eq 'SHELL') {
  223.     my $profile_track = $o->conf('profile-track');
  224.     chomp (my $startup = <<END);
  225. $NAME version $VERSION.
  226. Copyright (c) 2001 ActiveState SRL. All Rights Reserved.
  227.  
  228. Entering interactive shell.
  229. END
  230.     my $profile_tracking_warning = $profile_track ? '' : <<'END';
  231.  
  232. Profile tracking is not enabled. If you save and restore profiles manually,
  233. your profile may be out of sync with your computer. See 'help profile' for
  234. more information.
  235. END
  236.     $o->inform($startup);
  237.     $o->inform(<<END);
  238.  Using $o->{API}{readline} as readline library.
  239. $profile_tracking_warning
  240. Type 'help' to get started.
  241.  
  242. END
  243.     }
  244.     else {
  245.     $o->inform("$NAME ($VERSION). Type 'help' to get started.\n");
  246.     }
  247.  
  248.     $o->term->SetHistory(@{$o->conf('history') || []})
  249.     if $o->term->Features->{setHistory};
  250. }
  251.  
  252. sub postloop {
  253.     my $o = shift;
  254.     trace(1, "PPM: exiting...\n");
  255.     if ($o->mode eq 'SHELL' and $o->term->Features->{getHistory}) {
  256.     my @h = $o->term->GetHistory;
  257.     my $max_history = $o->conf('max_history') || 100;
  258.     splice @h, 0, (@h - $max_history)
  259.         if @h > $max_history;
  260.     my $old = $o->setmode('SILENT');
  261.     $o->conf('history', \@h);
  262.     $o->setmode($old);
  263.     }
  264. }
  265.  
  266. #============================================================================
  267. # Cache of search and query results
  268. #============================================================================
  269. sub cache_set_current {
  270.     my $o = shift;
  271.     my $type = shift;
  272.     my $set = shift;
  273.     $set = $o->{SHELL}{CACHE}{$type}{current} unless defined $set;
  274.     $o->{SHELL}{CACHE}{$type}{current} = $set;
  275.     return $o->{SHELL}{CACHE}{$type}{current};
  276. }
  277.  
  278. sub cache_set_index {
  279.     my $o = shift;
  280.     my $type = shift;
  281.     my $index = shift;
  282.     $index = $o->{SHELL}{CACHE}{$type}{index} unless defined $index;
  283.     $o->{SHELL}{CACHE}{$type}{index} = $index;
  284.     return $o->{SHELL}{CACHE}{$type}{index};
  285. }
  286.  
  287. sub cache_set_add {
  288.     my $o = shift;
  289.     my $type = shift;
  290.     my $query = shift;
  291.     my $entries = shift;
  292.     my $sort_field = $o->conf('sort-field');
  293.     my @sorted = $o->sort_pkgs($sort_field, @$entries);
  294.     my $set = {
  295.           query => $query,
  296.           raw => $entries,
  297.           $sort_field => \@sorted,
  298.         };
  299.     push @{$o->{SHELL}{CACHE}{$type}{sets}}, $set;
  300. }
  301.  
  302. sub cache_entry {
  303.     my $o = shift;
  304.     my $type = shift;        # 'query' or 'cache';
  305.     my $index = shift;        # defaults to currently selected index
  306.     my $set = shift;        # defaults to currently selected set
  307.  
  308.     $index = $o->{SHELL}{CACHE}{$type}{index} unless defined $index;
  309.  
  310.     my $src = $o->cache_set($type, $set);
  311.     return undef unless $src and bounded(0, $index, $#$src);
  312.  
  313.     # Make sure we display only valid entries:
  314.     my $tar = $o->conf('target');
  315.     $src->[$index]->make_complete($tar);
  316.     return $src->[$index];
  317. }
  318.  
  319. sub cache_set {
  320.     my $o = shift;
  321.     my $type = shift;        # 'query' or 'cache'
  322.     my $set = shift;        # defaults to currently selected set
  323.     my $entry = shift;        # defaults to 'results';
  324.  
  325.     $entry = $o->conf('sort-field') unless defined $entry;
  326.     return undef unless grep { lc($entry) eq $_ } (sort_fields(), 'query');
  327.  
  328.     $set = $o->{SHELL}{CACHE}{$type}{current} unless defined $set;
  329.     my $src = $o->{SHELL}{CACHE}{$type}{sets};
  330.  
  331.     return undef unless defined $set;
  332.     return undef unless bounded(0, $set, $#$src);
  333.  
  334.     # We've changed sort-field at some point -- make sure the sorted data
  335.     # exists, or else build it:
  336.     unless (defined $src->[$set]{$entry}) {
  337.     my $raw = $src->[$set]{raw};
  338.     my @sorted = $o->sort_pkgs($entry, @$raw);
  339.     $src->[$set]{$entry} = \@sorted;
  340.     }
  341.     
  342.     return wantarray ? @{$src->[$set]{$entry}} : $src->[$set]{$entry};
  343. }
  344.  
  345. sub cache_clear {
  346.     my $o = shift;
  347.     my $type = shift;        # 'query' or 'cache'
  348.     $o->{SHELL}{CACHE}{$type}{sets} = [];
  349.     $o->{SHELL}{CACHE}{$type}{current} = -1;
  350.     $o->{SHELL}{CACHE}{$type}{index} = -1;
  351. }
  352.  
  353. sub cache_sets {
  354.     my $o = shift;
  355.     my $type = shift;
  356.     @{$o->{SHELL}{CACHE}{$type}{sets}};
  357. }
  358.  
  359. # This sub searches for an entry in the cache whose name matches that thing
  360. # passed in. It searches in the current cache first. If the name isn't found,
  361. # it searches in all caches. If the name still isn't found, it returns undef.
  362. sub cache_find {
  363.     my $o = shift;
  364.     my $type = shift;
  365.     my $name = shift;
  366.  
  367.     my $ncaches = $o->cache_sets($type);
  368.     my $current = $o->cache_set_current($type);
  369.  
  370.     # First, search the current set:
  371.     my @pkgs = map { $_ ? $_->name : '' } $o->cache_set($type);
  372.     my $ind  = find_index($name, 0, @pkgs);
  373.     return ($current, $ind) if $ind >= 0;
  374.  
  375.     # Now try to find in all the sets:
  376.     for my $s (0 .. $ncaches - 1) {
  377.     next if $s == $current;
  378.     @pkgs = map { $_ ? $_->name : '' } $o->cache_set($type, $s);
  379.     $ind  = find_index($name, 0, @pkgs);
  380.     return ($s, $ind) if $ind >= 0;
  381.     }
  382.     return (-1, -1);
  383. }
  384.  
  385. # A pretty separator to print between logically separate items:
  386. my $SEP;
  387. BEGIN {
  388.     $SEP = '=' x 20;
  389. }
  390.  
  391. # Useful functions:
  392. sub max (&@) {
  393.     my $code = shift;
  394.     my $max;
  395.     local $_;
  396.     for (@_) {
  397.     my $res = $code->($_);
  398.     $max = $res if not defined $max or $max < $res;
  399.     }
  400.     $max || 0;
  401. }
  402.  
  403. sub min (&@) {
  404.     my $code = shift;
  405.     my $min;
  406.     local $_;
  407.     for (@_) {
  408.     my $res = $code->($_);
  409.     $min = $res if not defined $min or $min > $res;
  410.     }
  411.     $min || 0;
  412. }
  413.  
  414. sub sum (&@) {
  415.     my $code = shift;
  416.     my $sum = 0;
  417.     local $_;
  418.     for (@_) {
  419.     my $res = $code->($_);
  420.     $sum += $res if defined $res;
  421.     }
  422.     $sum || 0;
  423. }
  424.  
  425. #============================================================================
  426. # Repository:
  427. # rep            # displays repositories
  428. # rep add http://...    # adds a new repository
  429. # rep del <\d+>        # deletes the specified repository
  430. # rep [set] 1        # sets the specified repository active
  431. #============================================================================
  432. sub smry_repository { "adds, removes, or sets repositories" }
  433. sub help_repository { <<'END' }
  434. repository -- Repository Control
  435.   Synopsis
  436.  
  437.      rep                        Displays all repositories
  438.      rep add [name] <location>  Adds a new repository; makes it active
  439.      rep delete <name or num>   Deletes specified repository
  440.      rep describe <name or num> Displays information about the specified
  441.                                 repository
  442.      rep rename <name or num> <name>
  443.                                 Renames the specified repository to
  444.                                 the given name
  445.      rep on <name>              Activates the specified repository
  446.      rep off <name or num>      Removes the repository from the active list
  447.      rep up <name or num>       Moves the specified repository up one
  448.      rep down <name or num>     Moves the specified repository down one
  449.  
  450.   Description
  451.  
  452.     The *repository* (or *rep*) command controls two lists or repositories:
  453.  
  454.     1   The list of "active" repositories. This is the list of repositories
  455.         used by *search*, *install*, *upgrade*, and *verify*.
  456.  
  457.     2   The list of all known repositories. You can designate a repository
  458.         "inactive", which means PPM will not use it in any commands.
  459.  
  460.     If no arguments are given, the rep command will list the active
  461.     repositories defined in the PPM settings. The order is significant: when
  462.     installing a package, PPM will try the first repository, then the
  463.     second, and so on, until it find the package you asked for. When
  464.     searching, PPM merges the results of all the repositories together, so
  465.     the order is less important (see the *search* command).
  466.  
  467.     For example, when you enter:
  468.  
  469.         rep
  470.  
  471.     PPM3 will return something resembling this:
  472.  
  473.         Repositories:
  474.         [1] ActiveCD
  475.         [2] ActiveState Package Repository
  476.         [ ] An inactive repository
  477.  
  478.     In the example above, entering 'rep off 2' will disable the second
  479.     repository (the ActiveStat Package Repository). To add another
  480.     repository:
  481.  
  482.         rep add [options] <NAME> <LOCATION>
  483.  
  484.     The following options are available for the 'add' command:
  485.  
  486.         -username
  487.  
  488.         -password
  489.  
  490.     These options allow you to specify a username and password to be used
  491.     when logging into a repository. Currently, these are only used for FTP
  492.     and WWW repositories.
  493.  
  494.     For example:
  495.  
  496.         rep add "EZE" http://ppm.ActiveState.com/PPMPackages/5.6plus
  497.  
  498.     with "EZE" being the name of the repository (for easy reference) and the
  499.     location noted by the http location. If you were to enter the rep
  500.     command again, you would see:
  501.  
  502.         ppm> rep
  503.         Repositories:
  504.         [1] ActiveCD
  505.         [2] ActiveState Package Repository
  506.         [3] EZE
  507.         [ ] An inactive repository
  508.  
  509.     To move the new repository to the top of the Active list, you would
  510.     type:
  511.  
  512.         ppm> rep up EZE
  513.         Repositories:
  514.         [1] ActiveCD
  515.         [2] EZE
  516.         [3] ActiveState Package Repository
  517.         [ ] An inactive repository
  518.         ppm> rep up EZE
  519.         Repositories:
  520.         [1] EZE
  521.         [2] ActiveCD
  522.         [3] ActiveState Package Repository
  523.         [ ] An inactive repository
  524.  
  525.     To disable the ActiveCD repository temporarily, enter the following:
  526.  
  527.         ppm> rep off ActiveCD
  528.         Repositories:
  529.         [1] EZE
  530.         [2] ActiveState Package Repository
  531.         [ ] ActiveCD
  532.         [ ] An inactive repository
  533.  
  534.     To describe a repository, refer to it either by name, or by the number
  535.     displayed next to the repository in the Active Repositories list. You
  536.     must refer to inactive repositories by their full name.
  537.  
  538.         ppm> rep describe 2
  539.         Describing Active Repository 2:
  540.             Name: ActiveState Package Repository
  541.         Location: http://ppm.ActiveState.com/cgibin/PPM/...
  542.             Type: PPMServer 2.00
  543.         ppm> rep describe ActiveCD
  544.         Describing Inactive Repository:
  545.             Name: ActiveCD
  546.         Location: F:\PPMPackages\5.6plus
  547.             Type: Local Directory
  548.  
  549.     To re-activate the ActiveCD repository, use the *rep on* command. You
  550.     must refer to inactive repositories by name, not number.
  551.  
  552.         ppm> rep on ActiveCD
  553.         Active Repositories:
  554.         [1] EZE
  555.         [2] ActiveState Package Repository
  556.         [3] ActiveCD
  557.         [ ] An inactive repository
  558.  
  559.   Repository Types
  560.  
  561.     PPM3 supports several types of package repositories:
  562.  
  563.     1.  PPM Server 3.0
  564.  
  565.         ActiveState's SOAP-driven package server. Because all searches are
  566.         done server-side, the server can deliver much richer information
  567.         about packages than other repositories.
  568.  
  569.     2.  PPM Server 2.0
  570.  
  571.         The SOAP server designed for PPM version 2. PPM3 ships with the PPM2
  572.         repository as well as the PPM3 repository, so you can use either.
  573.         Simple searches are performed server-side. If your search is too
  574.         complicated for the server, PPM3 will download the package summary
  575.         and search by itself.
  576.  
  577.     3.  Web Repositories
  578.  
  579.         Older versions of PPM used non-SOAP repositories (directories full
  580.         of PPD files accessible using a web browser). Over the history of
  581.         PPM, there have been several different ways of organising the files
  582.         so that PPM can search for packages properly. PPM3 tries to download
  583.         a summary file first -- if that fails, it gets the directory index.
  584.         It parses the summary or the index, and caches it. Searches are done
  585.         from the cache.
  586.  
  587.     4.  FTP Repositories
  588.  
  589.         FTP is another way of exposing a directory full of PPD files. PPM3
  590.         consideres FTP repositories a subset of Web repositories. Treat them
  591.         as identical: PPM3 downloads the summary or the "index" (file
  592.         listing in this case), parses it, and then searches from it.
  593.  
  594.     5.  Local Repositories
  595.  
  596.         To support installing packages from the ActiveCD, a local directory
  597.         can be a repository. PPM3 searches the files in the directory. All
  598.         valid path formats are supported, including UNC paths.
  599. END
  600. sub comp_repository {
  601.     my $o = shift;
  602.     my ($word, $line, $start) = @_;
  603.     my @words = $o->line_parsed($line);
  604.     my $words = scalar @words;
  605.     my @reps = PPM::UI::repository_list()->result_l;
  606.     my $reps = @reps;
  607.     my @compls = qw(add delete describe rename set select);
  608.     push @compls, ($reps ? (1 .. $reps) : ()); 
  609.  
  610.     if ($words == 1 or $words == 2 and $start != length($line)) {
  611.     return $o->completions($word, \@compls);
  612.     }
  613.     if ($words == 2 or $words == 3 and $start != length($line)) {
  614.     return (readline::rl_filename_list($word))
  615.       if $words[1] eq 'add';
  616.     return $o->completions($word, [1 .. $reps])
  617.       if $o->completions($words[1], [qw(delete describe rename set select)]) == 1;
  618.     }
  619.     ();
  620. }
  621. sub reps_all {
  622.     my $o = shift;
  623.     my $l = PPM::UI::repository_list();
  624.     unless ($l->is_success) {
  625.     $o->warn($l->msg);
  626.     return () unless $l->ok;
  627.     }
  628.     $l->result_l;
  629. }
  630. sub reps_on {
  631.     my $o = shift;
  632.     return @{$o->conf('active_reps')};
  633. }
  634. sub reps_off {
  635.     my $o = shift;
  636.     my @reps = $o->reps_all;
  637.     my @reps_on = $o->reps_on;
  638.     my @off;
  639.     for my $r (@reps) {
  640.     push @off, $r unless grep { $_ eq $r } @reps_on;
  641.     }
  642.     @off;
  643. }
  644. sub rep_on {
  645.     my $o = shift;
  646.     my $rep = shift;
  647.     my @reps = ($o->reps_on, $rep);
  648.     my $m = $o->setmode('SILENT');
  649.     $o->conf('active_reps', \@reps);
  650.     $o->setmode($m);
  651. }
  652. sub rep_off {
  653.     my $o = shift;
  654.     my $rep = shift;
  655.     my @reps = grep { $_ ne $rep } $o->reps_on;
  656.     my $m = $o->setmode('SILENT');
  657.     $o->conf('active_reps', \@reps);
  658.     $o->setmode($m);
  659. }
  660. sub rep_ison {
  661.     my $o = shift;
  662.     my $rep = shift;
  663.     scalar grep { $_ eq $rep } $o->reps_on;
  664. }
  665. sub rep_isoff {
  666.     my $o = shift;
  667.     my $rep = shift;
  668.     scalar grep { $_ eq $rep } $o->reps_off;
  669. }
  670. sub rep_exists {
  671.     my $o = shift;
  672.     my $rep = shift;
  673.     scalar grep { $_ eq $rep } $o->reps_all;
  674. }
  675. sub rep_uniq {
  676.     my $o = shift;
  677.     my $rep = shift;
  678.     unless ($o->rep_exists($rep) or $rep =~ /^\d+$/) {
  679.     /\Q$rep\E/i and return $_ for $o->reps_all;
  680.     }
  681.     $rep;
  682. }
  683. sub rep_up {
  684.     my $o = shift;
  685.     my $rep = shift;
  686.     my @reps = $o->reps_on;
  687.     my $ind = find_index($rep, 0, @reps);
  688.     if (bounded(1, $ind, $#reps)) {
  689.     @reps = (
  690.         @reps[0 .. $ind - 2],
  691.         $rep,
  692.         $reps[$ind - 1],
  693.         @reps[$ind + 1 .. $#reps]
  694.     );
  695.     }
  696.     my $m = $o->setmode('SILENT');
  697.     $o->conf('active_reps', \@reps);
  698.     $o->setmode($m);
  699. }
  700. sub rep_down {
  701.     my $o = shift;
  702.     my $rep = shift;
  703.     my @reps = $o->reps_on;
  704.     my $ind = find_index($rep, 0, @reps);
  705.     if (bounded(0, $ind, $#reps - 1)) {
  706.     @reps = (
  707.         @reps[0 .. $ind - 1],
  708.         $reps[$ind + 1],
  709.         $rep,
  710.         @reps[$ind + 2 .. $#reps]
  711.     );
  712.     }
  713.     my $m = $o->setmode('SILENT');
  714.     $o->conf('active_reps', \@reps);
  715.     $o->setmode($m);
  716. }
  717. sub run_repository {
  718.     my $o = shift;
  719.     my @args = @_;
  720.     my (@reps, @reps_off, @reps_on);
  721.     my $refresh = sub {
  722.     @reps = $o->reps_all;
  723.     @reps_off = $o->reps_off;
  724.     @reps_on = $o->reps_on;
  725.     };
  726.     &$refresh;
  727.     trace(1, "PPM: repository @args\n");
  728.  
  729.     if (@args) {
  730.     my ($cmd, @args) = @args;
  731.     #=====================================================================
  732.     # add, delete, describe, rename commands:
  733.     #=====================================================================
  734.     if (matches($cmd, "add")) {
  735.         # Support for usernames and passwords.
  736.         my ($user, $pass);
  737.         {
  738.         local *ARGV;
  739.         @ARGV = @args;
  740.         GetOptions(
  741.             "username=s"    => \$user,
  742.             "password=s"    => \$pass,
  743.         );
  744.         @args = @ARGV;
  745.         }
  746.         $o->warn(<<END) and return unless (@args == 1 or @args == 2);
  747. repository: invalid 'add' command arguments. See 'help repository'.
  748. END
  749.         my $name = $args[0];
  750.         my $url  = $args[1];
  751.         if (@args == 1) {    # rep add http://...
  752.         $url = $name;
  753.         $name = 'Autonamed';
  754.         for (my $i=1; $i<=@reps; $i++) {
  755.             my $tmp = "$name $i";
  756.             $name = $tmp and last
  757.               unless (grep { $tmp eq $_ } @reps);
  758.         }
  759.         }
  760.         my $ok = PPM::UI::repository_add($name, $url, $user, $pass);
  761.         unless ($ok->is_success) {
  762.         $o->warn($ok->msg);
  763.         return unless $ok->ok;
  764.         }
  765.         $o->rep_on($name);
  766.     }
  767.     elsif (matches($cmd, "del|ete")) {
  768.         my $arg = $args[0];
  769.         my $gonner = $arg;
  770.         if ($arg =~ /^\d+$/) {
  771.         return unless $o->assert(
  772.             bounded(1, $arg, scalar @reps_on),
  773.             "no such active repository $arg"
  774.         );
  775.         $gonner = $reps_on[$arg - 1];
  776.         }
  777.         else {
  778.         $gonner = $o->rep_uniq($gonner);
  779.         return unless $o->assert(
  780.             $o->rep_exists($gonner),
  781.             "no such repository '$gonner'"
  782.         );
  783.         }
  784.         my $ok = PPM::UI::repository_del($gonner);
  785.         unless ($ok->is_success) {
  786.         $o->warn($ok->msg);
  787.         return unless $ok->ok;
  788.         }
  789.         $o->rep_off($gonner);
  790.     }
  791.     elsif (matches($cmd, "des|cribe")) {
  792.         my $arg = $args[0] || '1';
  793.         my $rep = $arg;
  794.         if ($arg =~ /^\d+$/) {
  795.         return unless $o->assert(
  796.             bounded(1, $arg, scalar @reps_on),
  797.             "no such active repository $arg"
  798.         );
  799.         $rep = $reps_on[$arg - 1];
  800.         }
  801.         else {
  802.         $rep = $o->rep_uniq($rep);
  803.         return unless $o->assert(
  804.             $o->rep_exists($rep),
  805.             "no such repository '$rep'"
  806.         );
  807.         }
  808.         my $info = PPM::UI::repository_info($rep);
  809.         unless ($info->is_success) {
  810.         $o->warn($info->msg);
  811.         return unless $info->ok;
  812.         }
  813.         my $type = $o->rep_ison($rep) ? "Active" : "Inactive";
  814.         my $num  = (
  815.         $o->rep_ison($rep)
  816.         ? " " . find_index($rep, 1, @reps_on)
  817.         : ""
  818.         );
  819.         my @info = $info->result_l;
  820.         my @keys = qw(Name Location Type);
  821.         push @keys, qw(Username) if @info >= 4;
  822.         push @keys, qw(Password) if @info >= 5;
  823.         $o->inform("Describing $type Repository$num:\n");
  824.         $o->print_pairs(\@keys, \@info);
  825.         return 1;
  826.     }
  827.     elsif (matches($cmd, 'r|ename')) {
  828.         my $arg = $args[0];
  829.         my $name = $args[1];
  830.         my $rep = $arg;
  831.         if ($arg =~ /^\d+$/) {
  832.         return unless $o->assert(
  833.             bounded(1, $arg, scalar @reps_on),
  834.             "no such active repository $arg"
  835.         );
  836.         $rep = $reps_on[$arg - 1];
  837.         }
  838.         else {
  839.         $rep = $o->rep_uniq($rep);
  840.         return unless $o->assert(
  841.             $o->rep_exists($rep),
  842.             "no such repository '$rep'"
  843.         );
  844.         }
  845.         my $ok = PPM::UI::repository_rename($rep, $name);
  846.         unless ($ok->is_success) {
  847.         $o->warn($ok->msg);
  848.         return unless $ok->ok;
  849.         }
  850.         $o->rep_on($name) if $o->rep_ison($rep);
  851.         $o->rep_off($rep);
  852.     }
  853.  
  854.     #=====================================================================
  855.     # On, off, up, and down commands:
  856.     #=====================================================================
  857.     elsif (matches($cmd, 'on')) {
  858.         my $rep = $o->rep_uniq($args[0]);
  859.         return unless $o->assert(
  860.         $o->rep_isoff($rep),
  861.         "no such inactive repository '$rep'"
  862.         );
  863.         $o->rep_on($rep);
  864.         $o->cache_clear('search');
  865.     }
  866.     elsif (matches($cmd, 'of|f')) {
  867.         my $arg = $args[0];
  868.         my $rep = $arg;
  869.         if ($arg =~ /^\d+$/) {
  870.         return unless $o->assert(
  871.             bounded(1, $arg, scalar @reps_on),
  872.             "no such active repository $arg"
  873.         );
  874.         $rep = $reps_on[$arg - 1];
  875.         }
  876.         else {
  877.         $rep = $o->rep_uniq($rep);
  878.         return unless $o->assert(
  879.             $o->rep_exists($rep),
  880.             "no such repository '$rep'"
  881.         );
  882.         }
  883.         $o->rep_off($rep);
  884.         $o->cache_clear('search');
  885.     }
  886.     elsif (matches($cmd, 'up')) {
  887.         my $arg = $args[0];
  888.         my $rep = $arg;
  889.         if ($arg =~ /^\d+$/) {
  890.         return unless $o->assert(
  891.             bounded(1, $arg, scalar @reps_on),
  892.             "no such active repository $arg"
  893.         );
  894.         $rep = $reps_on[$arg - 1];
  895.         }
  896.         else {
  897.         return unless $o->assert(
  898.             $o->rep_exists($rep),
  899.             "no such repository '$rep'"
  900.         );
  901.         }
  902.         $o->rep_up($rep);
  903.     }
  904.     elsif (matches($cmd, 'do|wn')) {
  905.         my $arg = $args[0];
  906.         my $rep = $arg;
  907.         if ($arg =~ /^\d+$/) {
  908.         return unless $o->assert(
  909.             bounded(1, $arg, scalar @reps_on),
  910.             "no such active repository $arg"
  911.         );
  912.         $rep = $reps_on[$arg - 1];
  913.         }
  914.         else {
  915.         return unless $o->assert(
  916.             $o->rep_exists($rep),
  917.             "no such repository '$rep'"
  918.         );
  919.         }
  920.         $o->rep_down($rep);
  921.     }
  922.  
  923.     else {
  924.         $o->warn(<<END) and return;
  925. No such repository command '$cmd'; see 'help repository'.
  926. END
  927.     }
  928.     }
  929.     &$refresh;
  930.     unless(@reps) {
  931.     $o->warn("No repositories. Use 'rep add' to add a repository.\n");
  932.     }
  933.     else {
  934.     my $i = 0;
  935.     my $count = @reps_on;
  936.     my $l = length($count);
  937.     $o->inform("Repositories:\n");
  938.     for my $r (@reps_on) {
  939.         my $n = sprintf("%${l}d", $i + 1);
  940.         $o->inform("[$n] $r\n");
  941.         $i++;
  942.     }
  943.     for my $r ($o->dictsort(@reps_off)) {
  944.         my $s = ' ' x $l;
  945.         $o->inform("[$s] $r\n");
  946.     }
  947.     }
  948.     1;
  949. }
  950.  
  951. #============================================================================
  952. # Search:
  953. # search        # displays previous searches
  954. # search <\d+>        # displays results of previous search
  955. # search <terms>    # executes a new search on the current repository
  956. #============================================================================
  957. sub smry_search { "searches for packages in a repository" }
  958. sub help_search { <<'END' }
  959. search -- Search for Packages
  960.   Synopsis
  961.  
  962.      search                Displays list of previous searches
  963.      search <number>       Displays results of search <number>
  964.      search <glob pattern> Performs a new search
  965.      search <field>=<glob> Searches for all packages matching the field.
  966.      search *              Displays all packages in the current repository
  967.  
  968.     The available fields are 'ABSTRACT', 'NAME', 'TITLE', 'AUTHOR', and
  969.     'VERSION'. 'NAME' is used when you do not specify a field.
  970.  
  971.   Description
  972.  
  973.     Use the search command to look through the repository for packages. PPM
  974.     version 3.0 provides powerful search functionality. For example:
  975.  
  976.     1.  Search for 'CGI' anywhere in the name:
  977.  
  978.           search CGI
  979.  
  980.         Example results:
  981.  
  982.           Apache-CGI
  983.           CGI-Application
  984.           CGI-ArgChecker
  985.  
  986.     2.  Search for 'CGI' at the beginning of the name:
  987.  
  988.           search CGI*
  989.  
  990.         Example results:
  991.  
  992.           CGI-ArgChecker
  993.           CGI-Application
  994.  
  995.     3.  Search for all modules authored by someone with 'smith' in their
  996.         name or email:
  997.  
  998.           search AUTHOR=smith 
  999.  
  1000.         Example results:
  1001.  
  1002.           Apache-ProxyPass
  1003.           Business-ISBN
  1004.  
  1005.     4.  Search for 'compress' anywhere in the abstract:
  1006.  
  1007.           search ABSTRACT=compress
  1008.  
  1009.         Example results:
  1010.  
  1011.           Apache-GzipChain
  1012.           IO-Zlib
  1013.  
  1014.     5.  Search for 'CGI' in the name, or 'web' in the abstract:
  1015.  
  1016.           search CGI or ABSTRACT=web
  1017.  
  1018.         Example results:
  1019.  
  1020.           CGI-XMLForm
  1021.           HTML-Clean
  1022.  
  1023.     6.  Search for 'XML' in the name and either 'parser' in the name or
  1024.         'pars' in the abstract, but not with 'XPath' in the name:
  1025.  
  1026.           search XML and (parser or ABSTRACT=pars) and not XPath
  1027.  
  1028.         Example results:
  1029.  
  1030.           XML-Node
  1031.           XML-Parser-EasyTree
  1032.  
  1033.     7.  PPM Server 3.0 repositories only: search by module name, even if
  1034.         unrelated to the containing package:
  1035.  
  1036.           search Data::Grove
  1037.                                 
  1038.         Example results:
  1039.  
  1040.           libxml-perl
  1041.  
  1042.     8.  Browse all packages in the repository:
  1043.  
  1044.           search *
  1045.  
  1046.         Example results:
  1047.  
  1048.           Affix-Infix2Postfix
  1049.           AI-Fuzzy
  1050.           [many more...]
  1051.  
  1052.     Recall previous searches using the 'search <number>' command. PPM3
  1053.     stores searches for each session until you exit PPM.
  1054.  
  1055.     Some package names or versions are too long to be displayed in the
  1056.     search results. If a name is too long, you will see a '~' (tilde) as the
  1057.     last visible character in the column. You can use *describe* to view
  1058.     detailed information about such packages.
  1059.  
  1060.   Search Results
  1061.  
  1062.     When you type a command like "search XML", PPM searches in each of the
  1063.     Active Repositories (see the *repository* command) for your package. The
  1064.     results are merged into one list, and duplicates (packages found in more
  1065.     than one repository) are hidden.
  1066.  
  1067.     You can control what fields PPM shows for each package. The fields each
  1068.     have a built-in weight, which is used to calculate how wide to make each
  1069.     field based on the width of your screen. Information that doesn't fit
  1070.     into a field is truncated, and a tilde ("~") character is displayed in
  1071.     the last column of the field.
  1072.  
  1073.     Let's get down to an example:
  1074.  
  1075.         ppm> search XML
  1076.         Searching in Active Repositories
  1077.             1. CGI-XMLForm           [0.10] Extension to CGI.pm which
  1078.             2. Data-DumpXML          [1.01] Dump arbitrary data structures
  1079.             3. DBIx-XML_RDB          [0.05] Perl extension for creating XML
  1080.             4. DBIx-XMLMessage       [0.03] XML Message exchange between DBI
  1081.             5. GoXML-XQI            [1.1.4] Perl extension for the XML Query
  1082.             6. Language-DATR-DATR2~ [0.901] manipulate DATR .dtr, XML, HTML,
  1083.             7. libxml-perl           [0.07] support for deeply nested
  1084.             8. Mail-FilterXML         [0.1] Undetermined
  1085.             9. Mail-XML              [0.03] Adds a toXML() method to
  1086.            10. Pod-XML               [0.93] Module to convert POD to XML
  1087.  
  1088.     As you can see, the three fields being displayed are:
  1089.  
  1090.     1   NAME
  1091.  
  1092.         The package name
  1093.  
  1094.     2   VERSION
  1095.  
  1096.         The package version
  1097.  
  1098.     3   ABSTRACT
  1099.  
  1100.         The package abstract
  1101.  
  1102.     You can customize the view somewhat. If you want to view the authors,
  1103.     but not the abstract, you can run the same *search* command after using
  1104.     *set* to change the fields:
  1105.  
  1106.         ppm> set fields="NAME VERSION AUTHOR"
  1107.         Setting 'fields' set to 'name version author'.
  1108.         ppm> search XML
  1109.         Using cached search result set 1.
  1110.             1. CGI-XMLForm         [0.10] Matt Sergeant (matt@sergeant.org)
  1111.             2. Data-DumpXML        [1.01] Gisle Aas (gisle@aas.no)
  1112.             3. DBIx-XML_RDB        [0.05] Matt Sergeant (matt@sergeant.org)
  1113.             4. DBIx-XMLMessage     [0.03] Andrei Nossov (andrein@andrein.com)
  1114.             5. GoXML-XQI          [1.1.4] Matthew MacKenzie (matt@goxml.com)
  1115.             6. Language-DATR-DAT~ [0.901] Lee Goddard (lgoddard@cpan.org)
  1116.             7. libxml-perl         [0.07] Ken MacLeod (ken@bitsko.slc.ut.us)
  1117.             8. Mail-FilterXML       [0.1] Matthew MacKenzie (matt@goxml.com)
  1118.             9. Mail-XML            [0.03] Matthew MacKenzie (matt@goxml.com)
  1119.            10. Pod-XML             [0.93] Matt Sergeant (matt@sergeant.org)
  1120.  
  1121.     You can change the order in which the results are sorted, and what
  1122.     columns are displayed. The settings *fields* and *sort-field* changes
  1123.     this. You can sort by any valid field name (even fields which are not
  1124.     displayed). See the *settings* command for the valid field names.
  1125.  
  1126.     PPM always hides "duplicate" results. It decides whether a result is
  1127.     duplicated based on the fields being displayed. If the same package is
  1128.     found in more than one repository, but you don't have the REPOSITORY
  1129.     field showing, PPM will only list the package once.
  1130. END
  1131. sub comp_search {()}
  1132. sub run_search {
  1133.     my $o = shift;
  1134.     my @args = @_;
  1135.     my $query = $o->raw_args || join ' ', @args;
  1136.     trace(1, "PPM: search @args\n\tquery='$query'\n");
  1137.     return unless $o->assert(
  1138.     scalar $o->reps_on,
  1139.     "you must activate a repository before searching."
  1140.     );
  1141.  
  1142.     # No args: show cached result sets
  1143.     unless (@args) {
  1144.     my @search_results = $o->cache_sets('search');
  1145.     my $search_result_current = $o->cache_set_current('search');
  1146.     if (@search_results) {
  1147.         $o->inform("Search Result Sets:\n");
  1148.         my $i = 0;
  1149.         for (@search_results) {
  1150.         $o->informf("%s%2d",
  1151.                $search_result_current == $i ? "*" : " ",
  1152.                $i + 1);
  1153.         $o->inform(". $_->{query}\n");
  1154.         $i++;
  1155.         }
  1156.     }
  1157.     else {
  1158.         $o->warn("No search result sets -- provide a search term.\n");
  1159.         return;
  1160.     }
  1161.     }
  1162.  
  1163.     # Args:
  1164.     else {
  1165.     # Show specified result set
  1166.     if ($query =~ /^\d+/) {
  1167.         my $set = int($query);
  1168.         my $s = $o->cache_set('search', $set - 1);
  1169.         unless ($set > 0 and defined $s) {
  1170.         $o->warn("No such search result set '$set'.\n");
  1171.         return;
  1172.         }
  1173.  
  1174.         $query = $o->cache_set('search', $set-1, 'query');
  1175.         $o->inform("Search Results Set $set ($query):\n");
  1176.         $o->print_formatted($s, $o->cache_set_index('search'));
  1177.         $o->cache_set_current('search', $set-1);
  1178.         $o->cache_set_index('search', -1);
  1179.     }
  1180.        
  1181.     # Query is the same as a previous query on the same repository: 
  1182.     # Use cached results and set them as default
  1183.     elsif(grep { $_->{query} eq $query } $o->cache_sets('search')) {
  1184.         my @entries = $o->cache_sets('search');
  1185.         for (my $i=0; $i<@entries; $i++) {
  1186.         if ($o->cache_set('search', $i, 'query') eq $query) {
  1187.             $o->inform("Using cached search result set ", $i+1, ".\n");
  1188.             $o->cache_set_current('search', $i);
  1189.             my $set = $o->cache_set('search');
  1190.             $o->print_formatted($set);
  1191.         }
  1192.         }
  1193.     }
  1194.  
  1195.     # Perform a new search
  1196.     else {
  1197.         my @rlist = $o->reps_on;
  1198.         my $targ = $o->conf('target');
  1199.         my $case = not $o->conf('case-sensitivity');
  1200.  
  1201.         $o->inform("Searching in Active Repositories\n");
  1202.         my $ok = PPM::UI::search(\@rlist, $targ, $query, $case);
  1203.         unless ($ok->is_success) {
  1204.         $o->warn($ok->msg);
  1205.         return unless $ok->ok;
  1206.         }
  1207.         my @matches = $ok->result_l;
  1208.         unless (@matches) {
  1209.         $o->warn("No matches for '$query'; see 'help search'.\n");
  1210.         return 1;
  1211.         }
  1212.         $o->cache_set_index('search', -1);
  1213.         $o->cache_set_add('search', $query, \@matches);
  1214.         $o->cache_set_current('search', scalar($o->cache_sets('search')) - 1);
  1215.         my @set = $o->cache_set('search');
  1216.         $o->print_formatted(\@set);
  1217.     }
  1218.     }
  1219.     1;
  1220. }
  1221. sub alias_search { qw(s) }
  1222.  
  1223. #============================================================================
  1224. # tree
  1225. # tree        # shows the dependency tree for the default/current pkg
  1226. # tree <\d+>    # shows dep tree for numbered pkg in current search set
  1227. # tree <pkg>    # shows dep tree for given package
  1228. # tree <url>    # shows dep tree for package located at <url>
  1229. # tree <glob>    # searches for matches
  1230. #============================================================================
  1231. sub smry_tree { "shows package dependency tree" }
  1232. sub help_tree { <<'END' }
  1233. tree -- Show Dependency Tree for Packages
  1234.   Synopsis
  1235.  
  1236.      tree                Displays the dependency-tree of the current
  1237.                          or default package
  1238.      tree <number>       Displays the dependency-tree of the given <number>
  1239.      tree <range>        Displays a <range> of dependency-trees
  1240.      tree <package name> Displays the dependency-tree of the named package
  1241.      tree <url>          Displays the dependency-tree for the
  1242.                          package at <url>
  1243.      tree <glob pattern> Performs a new search using <glob pattern>
  1244.  
  1245.   Description
  1246.  
  1247.     The tree command is used to show the "dependency tree" of a given
  1248.     package (additional packages that are required by the current package).
  1249.     For example:
  1250.  
  1251.         tree SOAP-lite
  1252.  
  1253.     returns:
  1254.  
  1255.         ====================
  1256.         SOAP-Lite 0.51
  1257.         |__MIME-tools 5.316
  1258.         |   |__MailTools 1.15
  1259.         |   \__IO-stringy 1.216
  1260.         |
  1261.         \__MIME-Lite 2.105
  1262.         ====================
  1263.  
  1264.     SOAP-Lite requires four other packages.
  1265.  
  1266.     When tree is called without a <name> or <number> switch, the command
  1267.     will return the dependency tree of the first package in the default
  1268.     search result. If there is no default search, you will be requested to
  1269.     use search to find a package.
  1270. END
  1271. sub comp_tree { goto &comp_describe }
  1272. sub run_tree {
  1273.     my $o = shift;
  1274.     my @args = @_;
  1275.     trace(1, "PPM: tree @args\n");
  1276.  
  1277.     # Check for anything that looks like a query. If it does, just
  1278.     # send it to search() instead.
  1279.     my $query = $o->raw_args || join ' ', @args;
  1280.     $query ||= '';
  1281.     if ($query and not PPM::UI::is_pkg($args[0]) and not parse_range($query)) {
  1282.     $o->inform("Wildcards detected; using 'search' instead...\n");
  1283.     return $o->run('search', @_);
  1284.     }
  1285.  
  1286.     # No Args: describes current index of current result set, or 1.
  1287.     unless (@args) {
  1288.     my @search_results = $o->cache_sets('search');
  1289.     my $search_result_current = $o->cache_set_current('search');
  1290.     unless (@search_results and
  1291.         bounded(0, $search_result_current, $#search_results)) {
  1292.         $o->warn("No search results to show dependency tree for -- " . 
  1293.           "use 'search' to find a package.\n");
  1294.         return;
  1295.     }
  1296.     else {
  1297.         my @res = $o->cache_set('search');
  1298.         my $npkgs = @res;
  1299.         $o->inform("$SEP\n");
  1300.         if ($o->cache_entry('search')) {
  1301.         my $n = $o->cache_set_index('search') + 1;
  1302.         $o->inform("Package $n:\n");
  1303.         $o->tree_pkg($o->cache_entry('search'));
  1304.         }
  1305.         elsif (defined $o->cache_entry('search', 0)) {
  1306.         $o->inform("Package 1:\n");
  1307.         $o->tree_pkg($o->cache_entry('search', 0));
  1308.         $o->cache_set_index('search', 0);
  1309.         }
  1310.         else {
  1311.         $o->inform("Search Results are empty -- use 'search' again.\n");
  1312.         }
  1313.         $o->inform("$SEP\n");
  1314.     }
  1315.     }
  1316.  
  1317.     # Args provided
  1318.     else {
  1319.  
  1320.     # Describe a particular number:
  1321.     if (my @r = parse_range(@args)) {
  1322.         my @search_results = $o->cache_sets('search');
  1323.         my $search_result_current = $o->cache_set_current('search');
  1324.         unless (bounded(0, $search_result_current, $#search_results)) {
  1325.         $o->inform("No search results to show dependency tree for -- " . 
  1326.           "use 'search' to find a package.\n");
  1327.         return;
  1328.         }
  1329.         else {
  1330.         for my $n (@r) {
  1331.             my $sr = $o->cache_set('search');
  1332.             $o->inform("$SEP\n");
  1333.             if (bounded(1, $n, scalar @$sr)) {
  1334.             $o->inform("Package $n:\n");
  1335.             $o->tree_pkg($o->cache_entry('search', $n-1));
  1336.             }
  1337.             else {
  1338.             $o->inform("No such package $n in result set.\n");
  1339.             }
  1340.             $o->cache_set_index('search', $n - 1);
  1341.         }
  1342.         $o->inform("$SEP\n");
  1343.         }
  1344.     }
  1345.  
  1346.     # Describe a particular package
  1347.     else {
  1348.         return unless $o->assert(
  1349.         scalar $o->reps_on,
  1350.         "No repositories -- use 'rep add' to add a repository.\n"
  1351.         );
  1352.         my $pkg =
  1353.           PPM::UI::describe([$o->reps_on], $o->conf('target'), $args[0]);
  1354.         unless ($pkg->is_success) {
  1355.         $o->warn($pkg->msg);
  1356.         return unless $pkg->ok;
  1357.         }
  1358.         if ($pkg->ok) {
  1359.         $o->inform("$SEP\n");
  1360.         $o->tree_pkg($pkg->result);
  1361.         $o->inform("$SEP\n");
  1362.         }
  1363.     }
  1364.     }
  1365.     1;
  1366. }
  1367.  
  1368. #============================================================================
  1369. # Describe:
  1370. # des        # describes default or current package
  1371. # des <\d+>    # describes numbered package in the current search set
  1372. # des <pkg>    # describes the named package (bypasses cached results)
  1373. # des <url>    # describes the package located at <url>
  1374. #============================================================================
  1375. sub smry_describe { "describes packages in detail" }
  1376. sub help_describe { <<'END' }
  1377. describe -- Describe Packages
  1378.   Synopsis
  1379.  
  1380.      des                Describes default/current package
  1381.      des <number>       Describes package <number> in the
  1382.                         current search set
  1383.      des <range>        Describes packages in the given 
  1384.                         <range> from the current search
  1385.      des <package name> Describes named package
  1386.      des <url>          Describes package located at <url>
  1387.      des <glob pattern> Performes a new search using <glob pattern>
  1388.  
  1389.   Description
  1390.  
  1391.     The describe command returns information about a package, including the
  1392.     name of the package, the author's name and a brief description (called
  1393.     an "Abstract") about the package. For example:
  1394.  
  1395.         describe libnet
  1396.  
  1397.     returns:
  1398.  
  1399.         ===============================
  1400.         Package 1
  1401.         Name: libnet
  1402.         Version: 1.07.03
  1403.         Author: Graham Barr
  1404.         Abstract: Collection of Network protocol modules
  1405.         Implementations:
  1406.                 1.sun4-solaris-thread-multi
  1407.                 2.i686-linux-thread-multi
  1408.                 3.MSWIn32-x86-multi-thread
  1409.         ===============================
  1410.  
  1411.     There are two modifiers to the describe command:
  1412.  
  1413.     -ppd
  1414.         Displays the raw PPD of the package.
  1415.  
  1416.     -dump
  1417.         The same as -ppd.
  1418.  
  1419.     When the describe command is called without arguments, it returns
  1420.     information about the first package in the current search. If there is
  1421.     no default search set, you will be prompted to use the search command to
  1422.     find a package.
  1423.  
  1424.     If describe is called with a numeric argument, that number is set as the
  1425.     default package and the information about that package is returned. If
  1426.     the number given doesn't exist, you will be prompted to use search to
  1427.     find a package. Also, you can use describe to get descriptions of
  1428.     several packages. For example:
  1429.  
  1430.         describe 4-7
  1431.  
  1432.     will return descriptions of packages 4 through 7 in the current search
  1433.     request. You can also enter:
  1434.  
  1435.         describe 3-4,10
  1436.  
  1437.     to get information on packages 3, 4 and 10.
  1438.  
  1439.     If you specify a URL as the argument to describe, PPM will describe the
  1440.     package located at the URL. The URL must point to a PPD file. The URL
  1441.     can also point to a PPD file on your computer.
  1442.  
  1443.     When the describe command is given a name with a wildcard (such as "*"
  1444.     or "?") it executes the search command with the given argument. For
  1445.     example, describe Tk* will return the name(s) of any packages that match
  1446.     the search parameters.
  1447.  
  1448.   See Also
  1449.  
  1450.     properties
  1451. END
  1452. sub comp_describe {
  1453.     my $o = shift;
  1454.     my ($word, $line, $start) = @_;
  1455.  
  1456.     # If no search results
  1457.     my $n_results = $o->cache_sets('search');
  1458.     my $n_current = $o->cache_set_current('search');
  1459.     return ()
  1460.       unless ($n_results and bounded(0, $n_current, $n_results - 1));
  1461.     my @words = $o->line_parsed($line);
  1462.  
  1463.     # If the previous word isn't a number or the command, stop.
  1464.     return ()
  1465.       if ($#words > 0 and
  1466.       $words[$#words] !~ /^\d+/ and
  1467.       $start == length($line) or 
  1468.       $#words > 1);
  1469.  
  1470.     # This is the most optimistic list:
  1471.     my @results = $o->cache_set('search');
  1472.     my $npkgs = @results;
  1473.     my @compls = (1 .. $npkgs);
  1474.  
  1475.     # If the previous word is a number, return only other numbers:
  1476.     return $o->completions($word, \@compls)
  1477.       if $words[$#words] =~ /^\d+/;
  1478.  
  1479.     # Either a number or the names of the packages
  1480.     push @compls, map { $_->name } @results;
  1481.     return $o->completions($word, \@compls);
  1482. }
  1483. sub run_describe {
  1484.     my $o = shift;
  1485.     my @args = @_;
  1486.     
  1487.     # Check for options:
  1488.     my $ppd;
  1489.     {
  1490.     local @ARGV = @args;
  1491.     GetOptions(ppd => \$ppd, dump => \$ppd);
  1492.     @args = @ARGV;
  1493.     }
  1494.  
  1495.     trace(1, "PPM: describe @args\n");
  1496.  
  1497.     # Check for anything that looks like a query. If it does, just
  1498.     # send it to search() instead.
  1499.     my $query = $o->raw_args || join ' ', @args;
  1500.     if ($query and not PPM::UI::is_pkg($args[0]) and not parse_range($query)) {
  1501.     $o->inform("Wildcards detected; using 'search' instead...\n");
  1502.     return $o->run('search', @_);
  1503.     }
  1504.  
  1505.     my $dumper = sub {
  1506.     my $o = shift;
  1507.     my $pkg_obj = shift;
  1508.     my $ppd = $pkg_obj->getppd($o->conf('target'))->result;
  1509.     $o->page($ppd);
  1510.     };
  1511.     my $displayer = $ppd ? $dumper : \&describe_pkg;
  1512.  
  1513.     # No Args: describes current index of current result set, or 1.
  1514.     unless (@args) {
  1515.     my @search_results = $o->cache_sets('search');
  1516.     my $search_result_current = $o->cache_set_current('search');
  1517.     unless (@search_results and
  1518.         bounded(0, $search_result_current, $#search_results)) {
  1519.         $o->warn("No search results to describe -- " . 
  1520.           "use 'search' to find a package.\n");
  1521.         return;
  1522.     }
  1523.     else {
  1524.         my @res = $o->cache_set('search');
  1525.         my $npkgs = @res;
  1526.         $o->inform("$SEP\n");
  1527.         if ($o->cache_entry('search')) {
  1528.         my $n = $o->cache_set_index('search') + 1;
  1529.         $o->inform("Package $n:\n");
  1530.         $o->$displayer($o->cache_entry('search'));
  1531.         }
  1532.         elsif (defined $o->cache_entry('search', 0)) {
  1533.         $o->inform("Package 1:\n");
  1534.         $o->$displayer($o->cache_entry('search', 0));
  1535.         $o->cache_set_index('search', 0);
  1536.         }
  1537.         else {
  1538.         $o->warn("Search Results are empty -- use 'search' again.\n");
  1539.         }
  1540.         $o->inform("$SEP\n");
  1541.     }
  1542.     }
  1543.  
  1544.     # Args provided
  1545.     else {
  1546.  
  1547.     # Describe a particular number:
  1548.     if (my @r = parse_range(@args)) {
  1549.         my @search_results = $o->cache_sets('search');
  1550.         my $search_result_current = $o->cache_set_current('search');
  1551.         unless (bounded(0, $search_result_current, $#search_results)) {
  1552.         $o->warn("No search results to describe -- " . 
  1553.           "use 'search' to find a package.\n");
  1554.         return;
  1555.         }
  1556.         else {
  1557.         for my $n (@r) {
  1558.             my $sr = $o->cache_set('search');
  1559.             $o->inform("$SEP\n");
  1560.             if (bounded(1, $n, scalar @$sr)) {
  1561.             $o->inform("Package $n:\n");
  1562.             $o->$displayer($o->cache_entry('search', $n-1));
  1563.             }
  1564.             else {
  1565.             $o->inform("No such package $n in result set.\n");
  1566.             }
  1567.             $o->cache_set_index('search', $n - 1);
  1568.         }
  1569.         $o->inform("$SEP\n");
  1570.         }
  1571.     }
  1572.  
  1573.     # Describe a particular package
  1574.     else {
  1575.         return unless $o->assert(
  1576.         scalar $o->reps_on,
  1577.         "No repositories -- use 'rep add' to add a repository.\n"
  1578.         );
  1579.         my ($set, $index) = $o->cache_find('search', $args[0]);
  1580.         my ($ok, $pkg);
  1581.         if ($index >= 0) {
  1582.         $o->cache_set_current('search', $set);
  1583.         $o->cache_set_index('search', $index);
  1584.         $pkg = $o->cache_entry('search');
  1585.         }
  1586.         else {
  1587.         $ok = PPM::UI::describe([$o->reps_on],
  1588.                     $o->conf('target'), $args[0]);
  1589.         unless ($ok->is_success) {
  1590.             $o->inform($ok->msg);
  1591.             return unless $ok->ok;
  1592.         }
  1593.         $pkg = $ok->result;
  1594.         $o->cache_set_add('search', $args[0], [$pkg]);
  1595.         my $last = $o->cache_sets('search') - 1;
  1596.         $o->cache_set_current('search', $last);
  1597.         $o->cache_set_index('search', 0);
  1598.         }
  1599.         $o->inform("$SEP\n");
  1600.         $o->$displayer($pkg);
  1601.         $o->inform("$SEP\n");
  1602.     }
  1603.     }
  1604.     1;
  1605. }
  1606.  
  1607. #============================================================================
  1608. # Install:
  1609. # i        # installs default or current package
  1610. # i <\d+>    # installs numbered package in current search set
  1611. # i <pkg>    # installs named package
  1612. # i <url>    # installs the package at <url>
  1613. #============================================================================
  1614. sub smry_install { "installs packages" }
  1615. sub help_install { <<'END' }
  1616. install -- Install Packages
  1617.   Synopsis
  1618.  
  1619.      install           Installs default package
  1620.      install <number>  Installs packages by a specific <number>
  1621.      install <range>   Installs packages in the given numeric <range>
  1622.      install <name>    Installs named package
  1623.      install <url>     Installs the package located at <url>
  1624.  
  1625.   Description
  1626.  
  1627.     The install command is used to install packages from the repository.
  1628.     Install packages by name or number (the number is given by the
  1629.     repository or search request), or set a default package using the
  1630.     describe command. You can specify a full URL to a PPD file; the URL may
  1631.     point to a PPD file on your computer.
  1632.  
  1633.     If you have profile tracking enabled, (see 'help profile') the current
  1634.     profile will be updated to include the newly installed package(s).
  1635.  
  1636.     The following modifiers can be used with the install command:
  1637.  
  1638.         -force
  1639.  
  1640.         -noforce
  1641.  
  1642.         -follow
  1643.  
  1644.         -nofollow
  1645.  
  1646.     The force and follow switches determine how packages are installed:
  1647.  
  1648.      FORCE       FOLLOW          RESULT
  1649.      false       false           Checks to see if the package is installed and
  1650.                                  if it is, installation stops. If there are any
  1651.                                  missing prerequisites, the installation will
  1652.                                  fail.
  1653.  
  1654.      false       true            Checks to see if the package is installed and
  1655.                                  if it is, installation stops. If there are any
  1656.                                  missing prerequisites, they are automatically
  1657.                                  installed. NOTE: this is the default setting
  1658.                                  when PPM is first installed.
  1659.  
  1660.      true        false           If the package is installed, PPM will
  1661.                                  reinstall the package. If there are any
  1662.                                  missing prerequisites, the installation will
  1663.                                  fail.
  1664.  
  1665.      true        true            If the package is installed, PPM will
  1666.                                  reinstall the package. All prerequisites are
  1667.                                  installed, missing or not.
  1668.     
  1669.     If you do not specify any options, install uses the default settings.
  1670.     Set or view the current defaults using the 'settings' command.
  1671.  
  1672.     For example:
  1673.  
  1674.         install foo
  1675.  
  1676.     will install the package named "foo", using the default settings.
  1677.     Over-ride the defaults using the install modifiers described above.
  1678.  
  1679.     For example:
  1680.  
  1681.         install foo -force
  1682.  
  1683.     will install the "foo" package, even if it has already been installed.
  1684.     If both -force and -follow are set to "true", all the prerequisites for
  1685.     any package you install will also be installed. For example, the
  1686.     installation of a tk-related package, like "tk-ach" which is 8.4 kB will
  1687.     be preceded by the installation of Tk, which is 1.7 MB.
  1688.  
  1689.     You can also install by package number. Package numbers are based on the
  1690.     current repository or current search request. For example:
  1691.  
  1692.         install 6
  1693.  
  1694.     installs package number 6. You can install more than one package at one
  1695.     time:
  1696.  
  1697.         install 3-5
  1698.  
  1699.     installs packages 3, 4 and 5. You can also type install 3-6,8 to install
  1700.     packages 3,4,5,6 and 8.
  1701.  
  1702.   See Also
  1703.  
  1704.     profile
  1705. END
  1706. sub comp_install { goto &comp_describe }
  1707. sub run_install {
  1708.     my $o = shift;
  1709.     my @args = @_;
  1710.     trace(1, "PPM: install @args\n");
  1711.  
  1712.     # Get the install options
  1713.     my %opts = (
  1714.     force  => $o->conf('force-install'),
  1715.     follow => $o->conf('follow-install'),
  1716.     dryrun => 0,
  1717.     );
  1718.     {
  1719.     local @ARGV = @args;
  1720.     GetOptions('force!'  => \$opts{force},
  1721.            'follow!' => \$opts{follow},
  1722.            'dryrun'  => \$opts{dryrun},
  1723.           );
  1724.     @args = @ARGV;
  1725.     }
  1726.  
  1727.     # No Args -- installs default package
  1728.     unless (@args) {
  1729.     my @search_results = $o->cache_sets('search');
  1730.     my $search_result_current = $o->cache_set_current('search');
  1731.     unless (@search_results and
  1732.         bounded(0, $search_result_current, $#search_results)) {
  1733.         $o->warn("No search results to install -- " . 
  1734.           "use 'search' to find a package.\n");
  1735.         return;
  1736.     }
  1737.     else {
  1738.         my @results = $o->cache_set('search');
  1739.         my $npkgs = @results;
  1740.         my $pkg;
  1741.         if ($o->cache_entry('search')) {
  1742.         my $n = $o->cache_set_index('search') + 1;
  1743.         $o->inform("Package $n:\n");
  1744.         $pkg = $o->cache_entry('search');
  1745.         }
  1746.         else {
  1747.         $o->inform("Package 1:\n");
  1748.         $pkg = $o->cache_entry('search', 0);
  1749.         }
  1750.         return $o->install_pkg($pkg, \%opts);
  1751.     }
  1752.     }
  1753.  
  1754.     # Args provided
  1755.     else {
  1756.  
  1757.     # Install a particular number:
  1758.     if (my @r = parse_range(@args)) {
  1759.         my @search_results = $o->cache_sets('search');
  1760.         my $search_result_current = $o->cache_set_current('search');
  1761.         unless (@search_results and
  1762.             bounded(0, $search_result_current, $#search_results)) {
  1763.         $o->warn("No search results to install -- " . 
  1764.           "use 'search' to find a package.\n");
  1765.         return;
  1766.         }
  1767.         else {
  1768.         my $ok = 0;
  1769.         for my $n (@r) {
  1770.             my $sr = $o->cache_set('search');
  1771.             if (bounded(1, $n, scalar @$sr)) {
  1772.             $o->inform("Package $n:\n");
  1773.             my $pkg = $sr->[$n-1];
  1774.             $ok++ if $o->install_pkg($pkg, \%opts);
  1775.             }
  1776.             else {
  1777.             $o->inform("No such package $n in result set.\n");
  1778.             }
  1779.         }
  1780.         return unless $ok;
  1781.         }
  1782.     }
  1783.  
  1784.     # Install a particular package
  1785.     else {
  1786.         if ($o->reps_on) {
  1787.         return $o->install_pkg($args[0], \%opts);
  1788.         }
  1789.         elsif ($o->reps_all) {
  1790.         $o->warn("Can't install: no repositories are active.\n");
  1791.         }
  1792.         else {
  1793.         $o->warn("Can't install: no repositories defined.\n");
  1794.         }
  1795.         return;
  1796.     }
  1797.     }
  1798.     1;
  1799. }
  1800.  
  1801. #============================================================================
  1802. # Target:
  1803. # t        # displays a list of backend targets
  1804. # t [set] <\d+>    # sets numbered target as default backend target
  1805. # t des [<\d+>]    # describes the given (or default) target
  1806. #============================================================================
  1807. sub smry_targets { "views or sets target installer backends" }
  1808. sub help_targets { <<'END' }
  1809. targets -- View Target Installer Backends
  1810.   Synopsis
  1811.  
  1812.      target                      Displays a list of backend targets
  1813.      target <number>             Sets <number> as default backend target
  1814.      target [select] <name or num>
  1815.                                  Sets <name or num> as default backend target
  1816.      target describe [name or num]
  1817.                                  Describes the given (or default) target
  1818.      target set <key> <val>      Sets the target's <key> to <val> 
  1819.      target rename <name or num> <name>
  1820.                                  Renames the given target to <name>
  1821.  
  1822.   Description
  1823.  
  1824.     The target is the destination location of the install routine, such as
  1825.     the directory where the packages are installed when they're downloaded
  1826.     from the repository. For example:
  1827.  
  1828.         target
  1829.  
  1830.     returns:
  1831.  
  1832.         Targets:
  1833.           1. ActivePerl 618
  1834.         * 2. ActivePerl 629
  1835.  
  1836.     This shows that there are two available targets, and that the second
  1837.     target (ActivePerl 629) is currently the default (as shown by the
  1838.     asterisk). Using multiple targets, you can manage multiple installations
  1839.     of Perl from a single command-line.
  1840. END
  1841. sub comp_targets {
  1842.     my $o = shift;
  1843.     my ($word, $line, $start) = @_;
  1844.     my @words = $o->line_parsed($line);
  1845.     my $words = scalar @words;
  1846.     my @compls;
  1847.     my @targs = PPM::UI::target_list()->result_l;
  1848.  
  1849.     # only return 'set' and 'describe' when we're completing the second word
  1850.     if ($words == 1 or $words == 2 and $start != length($line)) {
  1851.     @compls = ('set', 'select', 'describe', 'rename', 1 .. scalar @targs);
  1852.     return $o->completions($word, \@compls);
  1853.     }
  1854.  
  1855.     if ($words == 2 or $words == 3 and $start != length($line)) {
  1856.     # complete 'set'
  1857.     if (matches($words[1], 's|et')) {
  1858.         my $targ = $o->conf('target');
  1859.         @compls = map { $_->[0] }
  1860.               grep { $_->[1] }
  1861.               PPM::UI::target_config_keys($targ)->result_l;
  1862.         return $o->completions($word, \@compls);
  1863.     }
  1864.     # complete 'describe' and 'rename'
  1865.     elsif (matches($words[1], 'd|escribe')
  1866.         or matches($words[1], 'r|ename')
  1867.         or matches($words[1], 's|elect')) {
  1868.         return $o->completions($word, [1 .. scalar @targs]);
  1869.     }
  1870.     }
  1871.     ();
  1872. }
  1873. sub run_targets {
  1874.     my $o = shift;
  1875.     my @args = @_;
  1876.     trace(1, "PPM: target @args\n");
  1877.  
  1878.     my @targets = PPM::UI::target_list()->result_l;
  1879.     my $targets = @targets;
  1880.  
  1881.     # No arguments: print targets
  1882.     if (@args) {
  1883.     my ($cmd, @rest) = @args;
  1884.     if ($cmd =~ /^\d+$/
  1885.         or matches($cmd, 'se|lect')) {
  1886.         my $num =     $cmd =~ /^\d+$/        ? $cmd        :
  1887.             $rest[0] =~ /^\d+$/    ? $rest[0]    :
  1888.             do {
  1889.                 my $n = find_index($rest[0], 1, @targets);
  1890.                 if ($n < 1) {
  1891.                 $o->warn("No such target '$rest[0]'.\n");
  1892.                 return;
  1893.                 }
  1894.                 $n;
  1895.             };
  1896.  
  1897.         # QA the number: is it too high/low?
  1898.         unless(bounded(1, $num, $targets)) {
  1899.         $o->warn("No such target number '$num'.\n");
  1900.         return;
  1901.         }
  1902.         else {
  1903.         $o->conf('target', $targets[$num-1]);
  1904.         $o->cache_clear('query');
  1905.         }
  1906.     }
  1907.     elsif (matches($cmd, 'r|ename')) {
  1908.         my ($oldnum, $newname) = @rest;
  1909.         $oldnum =    $oldnum =~ /^\d+$/ ? $oldnum :
  1910.             do {
  1911.                 my $n = find_index($oldnum, 1, @targets);
  1912.                 if ($n < 1) {
  1913.                 $o->warn("No such target '$oldnum'.\n");
  1914.                 return;
  1915.                 };
  1916.                 $n;
  1917.             };
  1918.         unless (defined $oldnum && $oldnum =~ /^\d+$/) {
  1919.         $o->warn(<<END);
  1920. target: '$cmd' requires a numeric argument. See 'help $cmd'.
  1921. END
  1922.         return;
  1923.         }
  1924.         unless (bounded(1, $oldnum, $targets)) {
  1925.         $o->warn("No such target number '$oldnum'.\n");
  1926.         return;
  1927.         }
  1928.         unless (defined $newname and $newname) {
  1929.         $newname = '' unless defined $newname;
  1930.         $o->warn(<<END);
  1931. Target names must be non-empty: '$newname' is not a valid name.
  1932. END
  1933.         return;
  1934.         }
  1935.         
  1936.         my $oldname = $targets[$oldnum - 1];
  1937.         my $ret = PPM::UI::target_rename($oldname, $newname);
  1938.         $o->warn($ret->msg) unless $ret->ok;
  1939.         $o->conf('target', $newname)
  1940.           if $o->conf('target') eq $oldname;
  1941.         @targets = PPM::UI::target_list()->result_l;
  1942.         $targets = scalar @targets;
  1943.     }
  1944.     elsif (matches($cmd, "s|et")) {
  1945.         my ($key, $value) = @rest;
  1946.         if (defined $key and $key =~ /=/ and not defined $value) {
  1947.         ($key, $value) = split /=/, $key;
  1948.         }
  1949.         unless(defined($key) && $key) {
  1950.         $o->warn(<<END);
  1951. You must specify what option to set. See 'help target'.
  1952. END
  1953.         return;
  1954.         }
  1955.         unless(defined($value)) {
  1956.         $o->warn(<<END);
  1957. You must provide a value for the option. See 'help target'.
  1958. END
  1959.         return;
  1960.         }
  1961.         my $targ = $o->conf('target');
  1962.         my %keys = map { @$_ }
  1963.                PPM::UI::target_config_keys($targ)->result_l;
  1964.         unless ($keys{$key}) {
  1965.         $o->warn("Invalid set key '$key'; these are the settable values:\n");
  1966.         $o->warn("    $_\n") for (grep { $keys{$_} } keys %keys);
  1967.         return;
  1968.         }
  1969.         my $ok = PPM::UI::target_config_set($targ, $key, $value);
  1970.         unless ($ok->is_success) {
  1971.         $o->warn($ok->msg);
  1972.         return unless $ok->ok;
  1973.         }
  1974.         $o->inform("Target attribute '$key' set to '$value'\n");
  1975.         return 1;
  1976.     }
  1977.     elsif (matches($cmd, "d|escribe")) {
  1978.         my %opts = (exec => 1);
  1979.         my $sel;
  1980.         if (@rest) {
  1981.         local @ARGV = @rest;
  1982.         GetOptions(\%opts, 'exec!');
  1983.         @rest = @ARGV;
  1984.         }
  1985.         if (@rest) {
  1986.         $sel =    $rest[0] =~ /^\d+$/ ? $rest[0] :
  1987.                 do {
  1988.                 my $n = find_index($rest[0], 1, @targets);
  1989.                 if ($n < 1) {
  1990.                     $o->warn("No such target '$rest[0]'.\n");
  1991.                     return;
  1992.                 };
  1993.                 $n;
  1994.                 };
  1995.         unless(bounded(1, $sel, $targets)) {
  1996.             $o->warn("No such target number '$sel'.\n");
  1997.         }
  1998.         }
  1999.         else {
  2000.         $sel = find_index($o->conf('target'), 1, @targets);
  2001.         }
  2002.         my $targ = $targets[$sel-1];
  2003.         my (@keys, @vals);
  2004.         my $res = $opts{exec}
  2005.         ? PPM::UI::target_info($targ)
  2006.         : PPM::UI::target_raw_info($targ);
  2007.         unless ($res->is_success) {
  2008.         $o->warn($res->msg);
  2009.         return unless $res->ok;
  2010.         }
  2011.         my %h = $res->result_h;
  2012.         my @h = sort keys %h;
  2013.         push @keys, @h;
  2014.         push @vals, $h{$_} for @h;
  2015.         if ($opts{exec}) {
  2016.         for (PPM::UI::target_config_info($targ)->result_l) {
  2017.             push @keys, $_->[0];
  2018.             push @vals, $_->[1];
  2019.         }
  2020.         }
  2021.         $_ = ucfirst $_ for @keys;
  2022.         $o->inform("Describing target $sel ($targ):\n");
  2023.         $o->print_pairs(\@keys, \@vals);
  2024.         return 1;
  2025.     }
  2026.     }
  2027.     unless($targets) {
  2028.     $o->warn("No targets. Install a PPM target.\n");
  2029.     return;
  2030.     }
  2031.     else {
  2032.     $o->conf('target', $targets[0])
  2033.         unless $o->conf('target');
  2034.     my $i = 0;
  2035.     $o->inform("Targets:\n");
  2036.     for (@targets) {
  2037.         $o->informf(
  2038.         "%s%2d",
  2039.         $o->conf('target') eq $targets[$i] ? "*" : " ",
  2040.         $i + 1
  2041.         );
  2042.         $o->inform(". $_\n");
  2043.         $i++;
  2044.     }
  2045.     }
  2046.     1;
  2047. }
  2048.  
  2049. #============================================================================
  2050. # Query:
  2051. # query        # displays list of previous queries
  2052. # query <\d+>    # displays results of previous query
  2053. # query <terms>    # performs a new query and displays results
  2054. #============================================================================
  2055. sub smry_query { "queries installed packages" }
  2056. sub help_query { <<'END' }
  2057. query -- Query Installed Packages
  2058.   Synopsis
  2059.  
  2060.      query                   Displays list of previous queries
  2061.      query <number>          Displays results of previous query
  2062.      query <glob pattern>    Performs a new query using <glob pattern>
  2063.      query *                 Displays a list of all installed packages
  2064.  
  2065.   Description
  2066.  
  2067.     The query command displays a list of all installed packages, or a list
  2068.     based on the <glob pattern> switch. You can also check the list of past
  2069.     queries, or the results of a past query.
  2070.  
  2071.     With PPM 3.0, you can now perform much more powerful queries. The syntax
  2072.     is identical to the 'search' command, and almost all the search switches
  2073.     are also available for querying installed packages.
  2074.  
  2075.     Recall previous queries with the 'query <number>' command. PPM3 stores
  2076.     all queries from the current PPM session.
  2077.  
  2078.     Note: Depending on the value of the "case-sensitivity" setting, the
  2079.     query may or may not be case-sensitive. See "help settings" for
  2080.     instructions on setting the default case sensitivity.
  2081.  
  2082.   See Also
  2083.  
  2084.     search, settings
  2085. END
  2086. sub comp_query {()}
  2087. sub run_query {
  2088.     my $o = shift;
  2089.     my $query = $o->raw_args || join ' ', @_;
  2090.     trace(1, "PPM: query @_\n\tquery='$query'\n");
  2091.     my @targets = PPM::UI::target_list()->result_l;
  2092.     my $target = $o->conf('target');
  2093.     my $case = not $o->conf('case-sensitivity');
  2094.     $o->warn("You must install an installation target before using PPM.\n")
  2095.       and return unless @targets;
  2096.  
  2097.     # No args: show cached query sets
  2098.     unless ($query =~ /\S/) {
  2099.     my @query_results = $o->cache_sets('query');
  2100.     my $query_result_current = $o->cache_set_current('query');
  2101.     if (@query_results) {
  2102.         $o->inform("Query Result Sets:\n");
  2103.         my $i = 0;
  2104.         for (@query_results) {
  2105.         $o->informf("%s%2d",
  2106.                $query_result_current == $i ? "*" : " ",
  2107.                $i + 1);
  2108.         $o->inform(". $_->{query}\n");
  2109.         $i++;
  2110.         }
  2111.     }
  2112.     else {
  2113.         $o->warn("No query result sets -- provide a query term.\n");
  2114.         return;
  2115.     }
  2116.     }
  2117.  
  2118.     # Args:
  2119.     else {
  2120.     # Show specified result set 
  2121.     if ($query =~ /^\d+/) {
  2122.         my $set = int($query);
  2123.         unless (defined $o->cache_set('query', $set-1)) {
  2124.         $o->warn("No such query result set '$set'.\n");
  2125.         return;
  2126.         }
  2127.  
  2128.         $query = $o->cache_set('query', $set-1, 'query');
  2129.         $o->inform("Query Results Set $set ($query):\n");
  2130.         $o->print_formatted([$o->cache_set('query', $set-1)],
  2131.                 $o->cache_set_index('query'));
  2132.                 
  2133.         $o->cache_set_current('query', $set-1);
  2134.         $o->cache_set_index('query', -1);
  2135.     }
  2136.  
  2137.     # Query is the same a a previous query on the same target:
  2138.     # Use cached results and set them as default
  2139.     elsif (grep { $_->{query} eq $query } $o->cache_sets('query')) {
  2140.         for (my $i=0; $i<$o->cache_sets('query'); $i++) {
  2141.         if ($o->cache_set('query', $i, 'query') eq $query) {
  2142.             $o->inform("Using cached query result set ", $i+1, ".\n");
  2143.             $o->cache_set_current('query', $i);
  2144.             my $set = $o->cache_set('query');
  2145.             $o->print_formatted($set);
  2146.         }
  2147.         }
  2148.     }
  2149.  
  2150.     # Perform a new query.
  2151.     else {
  2152.         my $num = find_index($target, 1, @targets);
  2153.         $o->inform("Querying target $num (");
  2154.         if (length($target) > 30) {
  2155.         $o->inform(substr($target, 0, 30), "...");
  2156.         }
  2157.         else {
  2158.         $o->inform($target);
  2159.         }
  2160.         $o->inform(")\n");
  2161.  
  2162.         my $res = PPM::UI::query($target, $query, $case);
  2163.         unless ($res->ok) {
  2164.         $o->inform($res->msg);
  2165.         return;
  2166.         }
  2167.         my @matches = $res->result_l;
  2168.         if (@matches) {
  2169.         $o->cache_set_add('query', $query, \@matches);
  2170.         $o->cache_set_current('query', scalar($o->cache_sets('query')) - 1);
  2171.         my @set = $o->cache_set('query');
  2172.         $o->print_formatted(\@set);
  2173.         }
  2174.         else {
  2175.         $o->warn("No matches for '$query'; see 'help query'.\n");
  2176.         }
  2177.     }
  2178.     }
  2179.     1;
  2180. }
  2181.  
  2182. #============================================================================
  2183. # Properties:
  2184. # prop        # describes default installed package
  2185. # prop <\d+>    # describes numbered installed package
  2186. # prop <pkg>    # describes named installed package
  2187. # prop <url>    # describes installed package at location <url>
  2188. #============================================================================
  2189. sub smry_properties { "describes installed packages in detail" }
  2190. sub help_properties { <<'END' }
  2191. properties -- Describe Installed Packages
  2192.   Synopsis
  2193.  
  2194.      prop                    Describes default installed package
  2195.      prop <number>           Describes installed package <number>
  2196.      prop <range>            Describes a <range> of installed packages
  2197.      prop <package name>     Describes named installed package
  2198.      prop <url>              Describes installed package located at <url>
  2199.      prop <glob pattern>     Performs a new query using <glob pattern>
  2200.  
  2201.   Description
  2202.  
  2203.     The properties command is an verbose form of the describe command. In
  2204.     addition to summary information, properties will display the
  2205.     installation date and a URL showing the location of the package within
  2206.     the repository.
  2207.  
  2208.     If you specify the package as a URL, PPM determines the package name
  2209.     from the URL and searches for that.
  2210.  
  2211.     When the properties command is used with wildcard arguments, the text
  2212.     entered at the PPM prompt is passed to the query command.
  2213.  
  2214.     For example, typing 'properties libnet' will give you:
  2215.  
  2216.         ====================
  2217.             Name: libnet
  2218.          Version: 1.07.03
  2219.           Author: Graham Barr
  2220.            Title: libnet
  2221.         Abstract: Collection of Network protocol modules
  2222.         InstDate: Fri Oct  2 16:15:15 1998
  2223.         Location: http://ppm-ia.ActiveState.com/PPM/...
  2224.         ====================
  2225.  
  2226.   See Also
  2227.  
  2228.     describe
  2229. END
  2230. sub comp_properties {
  2231.     my $o = shift;
  2232.     my ($word, $line, $start) = @_;
  2233.  
  2234.     # If no query results
  2235.     my $n_results = scalar $o->cache_sets('query');
  2236.     my $n_current = $o->cache_set_current('query');
  2237.     unless ($n_results and bounded(0, $n_current, $n_results - 1)) {
  2238.     my $targ = $o->conf('target') or return ();
  2239.     my $r = PPM::UI::query($targ, '*');
  2240.     return () unless $r->ok;
  2241.     $o->cache_set_add('query', '*', $r->result);
  2242.     $o->cache_set_current('query', scalar($o->cache_sets('query')) - 1);
  2243.     }
  2244.     my @words = $o->line_parsed($line);
  2245.  
  2246.     # If the previous word isn't a number or the command, stop.
  2247.     return ()
  2248.       if ($#words > 0 and
  2249.       $words[$#words] !~ /^\d+/ and
  2250.       $start == length($line) or 
  2251.       $#words > 1);
  2252.  
  2253.     # This is the most optimistic list:
  2254.     my @results = $o->cache_set('query');
  2255.     my $npkgs = @results;
  2256.     my @compls = (1 .. $npkgs);
  2257.  
  2258.     # If the previous word is a number, return only other numbers:
  2259.     return $o->completions($word, \@compls)
  2260.       if ($words[$#words] =~ /^\d+/);
  2261.  
  2262.     # Either a number or the names of the packages
  2263.     push @compls, map { $_->name } @results;
  2264.     return $o->completions($word, \@compls);
  2265. }
  2266. sub run_properties {
  2267.     my $o = shift;
  2268.     my @args = @_;
  2269.     my $args = $args[0];
  2270.     trace(1, "PPM: properties @args\n");
  2271.  
  2272.     # Check for anything that looks like a query. If it does, send it
  2273.     # to query instead.
  2274.     my $query = $o->raw_args || join ' ', @args;
  2275.     $query ||= '';
  2276.     if ($query and not PPM::UI::is_pkg($args[0]) and not parse_range($query)) {
  2277.     $o->inform("Wildcards detected; using 'query' instead.\n");
  2278.     return $o->run('query', @_);
  2279.     }
  2280.     
  2281.     # No Args: describes current index of current result set, or 1.
  2282.     my $n_results = $o->cache_sets('query');
  2283.     my $n_current = $o->cache_set_current('query');
  2284.     my $ind = $o->cache_set_index('query');
  2285.     unless (@args) {
  2286.     unless ($n_results and bounded(0, $n_current, $n_results - 1)) {
  2287.         $o->inform("No query results to describe -- " . 
  2288.           "use 'query' to find a package.\n");
  2289.         return;
  2290.     }
  2291.     else {
  2292.         my @results = $o->cache_set('query');
  2293.         my $npkgs = @results;
  2294.         $o->inform("$SEP\n");
  2295.         if (bounded(0, $ind, $npkgs-1)) {
  2296.         my $n = $ind + 1;
  2297.         $o->inform("Package $n:\n");
  2298.         $o->describe_pkg($o->cache_entry('query', $ind));
  2299.         }
  2300.         else {
  2301.         $o->inform("Package 1:\n");
  2302.         $o->describe_pkg($results[0]);
  2303.         $o->cache_set_index('query', 0);
  2304.         }
  2305.         $o->inform("$SEP\n");
  2306.     }
  2307.     }
  2308.  
  2309.     # Args provided
  2310.     else {
  2311.  
  2312.     # Describe a particular number:
  2313.     if (my @r = parse_range(@args)) {
  2314.         unless ($n_results and bounded(0, $n_current, $n_results - 1)) {
  2315.         $o->inform("No query results to describe -- " . 
  2316.           "use 'query' to find a package.\n");
  2317.         return;
  2318.         }
  2319.         else {
  2320.         for my $n (@r) {
  2321.             my @results = $o->cache_set('query');
  2322.             my $npkgs = @results;
  2323.             $o->inform("$SEP\n");
  2324.             if (bounded(1, $n, $npkgs)) {
  2325.             $o->inform("Package $n:\n");
  2326.             $o->cache_set_index('query', $n-1);
  2327.             my $old = $o->cache_entry('query');
  2328.             my $prop =
  2329.               PPM::UI::properties($o->conf('target'), $old->name);
  2330.             unless ($prop->is_success) {
  2331.                 $o->warn($prop->msg);
  2332.                 next unless $prop->ok;
  2333.             }
  2334.             my ($pkg, $idate, $loc) = $prop->result_l;
  2335.             $o->describe_pkg($pkg,
  2336.                      [qw(InstDate Location)],
  2337.                      [$idate, $loc],
  2338.                     );
  2339.             }
  2340.             else {
  2341.             $o->inform("No such package $n in result set.\n");
  2342.             }
  2343.         }
  2344.         $o->inform("$SEP\n");
  2345.         }
  2346.     }
  2347.  
  2348.     # Query a particular package
  2349.     else {
  2350.         if ($o->conf('target')) {
  2351.         my $prop =
  2352.           PPM::UI::properties($o->conf('target'), $args);
  2353.         unless ($prop->is_success) {
  2354.             $o->warn($prop->msg);
  2355.             return unless $prop->ok;
  2356.         }
  2357.         my ($pkg, $idate, $loc) = $prop->result_l;
  2358.         my ($s, $index) = $o->cache_find('query', $args);
  2359.         $o->inform("$SEP\n") if $pkg;
  2360.         $o->describe_pkg($pkg,
  2361.                  [qw(InstDate Location)],
  2362.                  [$idate, $loc],
  2363.                 )
  2364.           if $pkg;
  2365.         $o->inform("$SEP\n") if $pkg;
  2366.         if ($index >= 0) {
  2367.             $o->cache_set_current('query', $s);
  2368.             $o->cache_set_index('query', $index);
  2369.         }
  2370.         elsif ($pkg) {
  2371.             $o->cache_set_add('query', $args[0], [$pkg]);
  2372.             my $last = $o->cache_sets('query') - 1;
  2373.             $o->cache_set_current('query', $last);
  2374.             $o->cache_set_index('query', 0);
  2375.         }
  2376.         $o->warn("Package '$args' not found; 'query' for it first.\n")
  2377.           and return unless $pkg;
  2378.         }
  2379.         else {
  2380.         # XXX: Change this output.
  2381.         $o->warn(
  2382.             "There are no targets installed.\n"
  2383.         );
  2384.         return;
  2385.         }
  2386.     }
  2387.     }
  2388.     1;
  2389. }
  2390.  
  2391. #============================================================================
  2392. # Uninstall:
  2393. # uninst    # removes default installed package
  2394. # uninst <\d+>    # removes specified package
  2395. # uninst <pkg>    # removes specified package
  2396. # uninst <url>    # removes the package located at <url>
  2397. #============================================================================
  2398. sub smry_uninstall { "uninstalls packages" }
  2399. sub help_uninstall { <<'END' }
  2400. remove, uninstall -- Uninstalls Installed Packages
  2401.   Synopsis
  2402.  
  2403.      remove              Deletes default installed package
  2404.      remove <number>     Deletes installed package <number>
  2405.      remove <range>      Deletes a <range> of installed packages
  2406.      remove <name>       Deletes a packages by a specific name
  2407.      remove <url>        Deletes the package located at <url>
  2408.  
  2409.   Description
  2410.  
  2411.     The remove and uninstall commands function identically. They are used to
  2412.     delete packages from the current target (specified using the target
  2413.     command). If profile tracking is enabled, (see 'help profile') the
  2414.     current PPM profile on ASPN will be updated.
  2415.  
  2416.     Packages can be removed by package name, by their numerical listing, or
  2417.     by specifying a URL to a PPD file. For example:
  2418.  
  2419.         remove XML-DOM
  2420.  
  2421.     will delete the XML-DOM package from the target.
  2422.  
  2423.     To remove package by number:
  2424.  
  2425.         remove 6
  2426.  
  2427.     and the sixth package in your current query will be removed. If no
  2428.     queries have been run in the current PPM session, you will be prompted
  2429.     to use a query to find a package before deleting it. Remember that
  2430.     removing packages clears all previous query requests, since the
  2431.     numerical sequence stored in any query will no longer be true once
  2432.     package(s) have been removed.
  2433.  
  2434.     Packages can also be removed in groups. For example:
  2435.  
  2436.         remove 4-7
  2437.  
  2438.     will delete packages 4, 5, 6, and 7 from your target. You can also skip
  2439.     packages:
  2440.  
  2441.         remove 3-5, 7
  2442.  
  2443.     this will delete packages 3, 4, 5 and 7, but will leave 6 intact.
  2444.     Remember to run a new query whenever you remove a package from your
  2445.     target.
  2446.  
  2447.     If you specify the package as a URL, PPM determines the package name
  2448.     from the URL and removes that.
  2449.  
  2450.     Please note that wildcards like "*" or "?" cannot be used with the
  2451.     remove command.
  2452.  
  2453.   See Also
  2454.  
  2455.     profile
  2456. END
  2457. sub comp_uninstall { goto &comp_properties; }
  2458. sub run_uninstall {
  2459.     my $o = shift;
  2460.     my @args = @_;
  2461.     trace(1, "PPM: uninstall @args\n");
  2462.  
  2463.     # Get the force option:
  2464.     my ($force);
  2465.     {
  2466.     local @ARGV = @args;
  2467.     GetOptions(
  2468.         'force!' => \$force,
  2469.     );
  2470.     @args = @ARGV;
  2471.     }
  2472.     
  2473.     my $args = $args[0];
  2474.  
  2475.     # No Args -- removes default package
  2476.     my $n_results = $o->cache_sets('query');
  2477.     my $n_current = $o->cache_set_current('query');
  2478.     my $ind = $o->cache_set_index('query');
  2479.     unless (@args) {
  2480.     unless ($n_results and bounded(0, $n_current, $n_results - 1)) {
  2481.         $o->warn("No query results to uninstall -- " . 
  2482.           "use 'query' to find a package.\n");
  2483.         return;
  2484.     }
  2485.     else {
  2486.         my @results = $o->cache_set('query');
  2487.         if (bounded(0, $ind, $#results)) {
  2488.         my $n = $ind + 1;
  2489.         $o->inform("Package $n:\n");
  2490.         $o->remove_pkg($o->cache_entry('query', $ind)->name, $force);
  2491.         }
  2492.         else {
  2493.         $o->inform("Package 1:\n");
  2494.         $o->remove_pkg($o->cache_entry('query', 0)->name, $force);
  2495.         }
  2496.     }
  2497.     }
  2498.  
  2499.     # Args provided
  2500.     else {
  2501.     # Uninstall a particular number:
  2502.     if (my @r = parse_range(@args)) {
  2503.         unless ($n_results and bounded(0, $n_current, $n_results - 1)) {
  2504.         $o->warn("No query results to uninstall -- " . 
  2505.           "use 'query' to find a package.\n");
  2506.         return;
  2507.         }
  2508.         else {
  2509.         my @results = $o->cache_set('query');
  2510.         my $npkgs = @results;
  2511.         my $ok = 0;
  2512.         for my $n (@r) {
  2513.             if (bounded(1, $n, $npkgs)) {
  2514.             $o->inform("Package $n:\n");
  2515.             $ok |=
  2516.               $o->remove_pkg($o->cache_entry('query', $n-1)->name,
  2517.                      $force, 1);
  2518.             }
  2519.             else {
  2520.             $o->warn("No such package $n in result set.\n");
  2521.             }
  2522.         }
  2523.         $o->cache_clear('query') if $ok;
  2524.         }
  2525.     }
  2526.  
  2527.     # Uninstall a particular package
  2528.     else {
  2529.         if ($o->conf('target')) {
  2530.         $o->remove_pkg($_, $force) for @args;
  2531.         }
  2532.         else {
  2533.         print
  2534.           "No targets -- use 'rep add' to add a target.\n";
  2535.         return;
  2536.         }
  2537.     }
  2538.     }
  2539.     1;
  2540. }
  2541. sub alias_uninstall { qw(remove) }
  2542.  
  2543. #============================================================================
  2544. # Settings:
  2545. #============================================================================
  2546. my (%lib_keys, @ui_keys);
  2547. my (@path_keys, @boolean_keys, @integer_keys);
  2548. my (%cache_clear_keys);
  2549. BEGIN {
  2550.     %lib_keys = ('download-chunksize' => 'downloadbytes',
  2551.         'tempdir' => 'tempdir',
  2552.         'trace-file' => 'tracefile',
  2553.         'trace-level' => 'tracelvl',
  2554.         'profile-track' => 'profile_enable',
  2555.         );
  2556.     @ui_keys = qw(
  2557.     case-sensitivity
  2558.     pager
  2559.     fields
  2560.     follow-install
  2561.     force-install
  2562.     prompt-context
  2563.     prompt-slotsize
  2564.     prompt-verbose
  2565.     sort-field
  2566.     verbose-startup
  2567.  
  2568.     install-verbose
  2569.     upgrade-verbose
  2570.     remove-verbose
  2571.     );
  2572.     @boolean_keys = qw(case-sensitivity force-install follow-install
  2573.                prompt-context prompt-verbose profile-track
  2574.                verbose-startup install-verbose upgrade-verbose
  2575.                remove-verbose
  2576.               );
  2577.     @integer_keys = qw(download-chunksize prompt-slotsize trace-level);
  2578.     @path_keys = qw(tempdir pager trace-file);
  2579.     @cache_clear_keys{qw/
  2580.     case-sensitivity
  2581.     /} = ();
  2582. }
  2583. sub settings_getkeys {
  2584.     my $o = shift;
  2585.     my @keys = @ui_keys;
  2586.     push @keys, keys %lib_keys;
  2587.     @keys;
  2588. }
  2589. sub settings_getvals {
  2590.     my $o = shift;
  2591.     my @vals;
  2592.     push @vals, $o->settings_getkey($_) for $o->settings_getkeys;
  2593.     @vals;
  2594. }
  2595.  
  2596. sub conf {
  2597.     my $o   = shift;
  2598.     my $key = shift;
  2599.     my $val = shift;
  2600.     my $un  = shift;
  2601.     return $o->settings_setkey($key, $val, $un) if defined $val;
  2602.     return $o->settings_getkey($key);
  2603. }
  2604.  
  2605. sub settings_getkey {
  2606.     my $o = shift;
  2607.     my $key = shift;
  2608.     return PPM::UI::config_get($lib_keys{$key})->result if $lib_keys{$key};
  2609.     return $o->{SHELL}{conf}{DATA}{$key};
  2610. }
  2611. sub settings_setkey {
  2612.     my $o = shift;
  2613.     my ($key, $val, $un) = @_;
  2614.     if (grep { $key eq $_ } @boolean_keys) {
  2615.     $val = 0 if $un;
  2616.     unless ($val =~ /^\d+$/ && ($val == 0 || $val == 1)) {
  2617.         $o->warn(<<END);
  2618. Setting '$key' must be boolean: '0' or '1'. See 'help settings'.
  2619. END
  2620.         return;
  2621.     }
  2622.     }
  2623.     elsif (grep { $key eq $_ } @integer_keys) {
  2624.     $val = 0 if $un;
  2625.     unless ($val =~ /^\d+$/) {
  2626.         $o->warn(<<END);
  2627. Setting '$key' must be numeric. See 'help settings'.
  2628. END
  2629.         return;
  2630.     }
  2631.     }
  2632.     elsif ($key eq 'sort-field') {
  2633.     $val = 'name' if $un;
  2634.     my @fields = sort_fields();
  2635.     unless (grep { lc($val) eq $_ } @fields) {
  2636.         $o->warn(<<END);
  2637. Error setting '$key' to '$val': should be one of:
  2638. @fields.
  2639. END
  2640.         return;
  2641.     }
  2642.     else {
  2643.         $val = lc($val);
  2644.         $o->cache_set_index('search', -1); # invalidates current indices.
  2645.         $o->cache_set_index('query', -1);
  2646.     }
  2647.     }
  2648.     elsif ($key eq 'fields') {
  2649.     $val = 'name version abstract' if $un;
  2650.     my @fields = sort_fields();
  2651.     my @vals = split ' ', $val;
  2652.     for my $v (@vals) {
  2653.         unless (grep { lc $v eq lc $_ } @fields) {
  2654.         $o->warn(<<END);
  2655. Error adding field '$v': should be one of:
  2656. @fields.
  2657. END
  2658.         return;
  2659.         }
  2660.     }
  2661.     $val = lc $val;
  2662.     }
  2663.  
  2664.     if ($un and $key eq 'tempdir') {
  2665.     $o->warn("Can't unset 'tempdir': use 'set' instead.\n");
  2666.     return;
  2667.     }
  2668.  
  2669.     # Check for any cache-clearing that needs to happen:
  2670.     if (exists $cache_clear_keys{$key}) {
  2671.     $o->cache_clear('search');
  2672.     $o->cache_clear('query');
  2673.     }
  2674.  
  2675.     if ($lib_keys{$key}) { PPM::UI::config_set($lib_keys{$key}, $val) }
  2676.     else {
  2677.     $o->{SHELL}{conf}{DATA}{$key} = $val;
  2678.     $o->{SHELL}{conf}->save;
  2679.     }
  2680.     $o->inform(<<END);
  2681. Setting '$key' set to '$val'.
  2682. END
  2683. }
  2684.  
  2685. sub smry_settings { "view or set PPM options" }
  2686. sub help_settings { <<'END' }
  2687. settings -- View or Set PPM Settings
  2688.   Synopsis
  2689.  
  2690.      set                 Displays current settings
  2691.      set <name>          Displays the current setting of the given <name>
  2692.      set <name> <value>  Sets <name> to <value>
  2693.      unset <name>        Sets <name> to a "false" value: '0' for boolean
  2694.                          Settings, '' for others.
  2695.  
  2696.   Description
  2697.  
  2698.     The settings command is used to configure the default PPM environment.
  2699.     Settings such as the number of lines displayed per page,
  2700.     case-sensitivity, and the log file are configured using the settings
  2701.     command.
  2702.  
  2703.     Setting names may be abbreviated to uniqueness. For example, instead of
  2704.     typing 'case-sensitivity', you may type 'case'.
  2705.  
  2706.     Available settings:
  2707.  
  2708.      NAME                VALUE           DESCRIPTION
  2709.      case-sensitivity    1 or 0      If 1, searches and queries are
  2710.                                      case-sensitive.
  2711.  
  2712.      download-chunksize  integer     If this is set to a positive,
  2713.                                      non-zero integer, PPM updates the
  2714.                                      status after "integer" of bytes
  2715.                                      transferred during an install or
  2716.                                      upgrade.
  2717.  
  2718.      fields              fields      A space-separated list of fields to 
  2719.                                      display in the search results. Valid
  2720.                                      fields are:
  2721.  
  2722.                                        ABSTRACT
  2723.                                        AUTHOR
  2724.                                        NAME
  2725.                                        REPOSITORY
  2726.                                        TITLE
  2727.                                        VERSION
  2728.  
  2729.                                      Usually, NAME and TITLE have the same
  2730.                                      content.
  2731.  
  2732.      follow-install      1 or 0      See 'help install' for details.
  2733.  
  2734.      force-install       1 or 0      See 'help install' for details.
  2735.  
  2736.      install-verbose     1 or 0      If 0, suppresses most output when
  2737.                                      installing packages. If 1, PPM prints
  2738.                                      each file as it is installed.
  2739.  
  2740.      pager               path        The path to an external pager program
  2741.                                      used to page long displays. If blank,
  2742.                                      or set to 'internal', the internal
  2743.                                      pager is used. If 'none', paging
  2744.                                      is disabled.
  2745.  
  2746.      profile-track       1 or 0      If 1, PPM arranges to have the 
  2747.                                      ASPN server track your PPM profile. 
  2748.                                      This means that every time your install
  2749.                                      or remove a package, your profile is
  2750.                                      updated on the server. If 0, you must
  2751.                                      manually save your profile using
  2752.                                      'profile save'.
  2753.  
  2754.      prompt-context      1 or 0      If 1, enables the prompt to change
  2755.                                      based on the current state of PPM, i.e
  2756.                                      showing current target, query, etc.
  2757.  
  2758.      prompt-slotsize     integer     If prompt-verbose is 1, this defines
  2759.                                      the width of each slot in the prompt.
  2760.                                      For instance, 4 means to use 4 
  2761.                                      character-wide slots.
  2762.  
  2763.      prompt-verbose      1 or 0      If 0, uses numbers to represent the
  2764.                                      context in the prompt; much shorter.
  2765.                                      If prompt-context is set to 0, there
  2766.                                      will be no visible difference in the
  2767.                                      'prompt-verbose' settings.
  2768.  
  2769.      remove-verbose      1 or 0      If 0, suppresses most output when
  2770.                                      removing packages. If 1, prints the
  2771.                                      name of each file as it is removed.
  2772.  
  2773.      sort-field          field       The field by which to sort search and
  2774.                                      query results. Valid fields are
  2775.                                      ABSTRACT, AUTHOR, NAME, TITLE
  2776.                                      and VERSION.
  2777.  
  2778.      tempdir             path        A temporary directory into which
  2779.                                      packages are downloaded and expanded
  2780.                                      during 'install' and 'upgrade'.
  2781.  
  2782.      trace-file          path        A file to which PPM will write tracing
  2783.                                      information.
  2784.  
  2785.      trace-level         integer     If 0 or negative, tracing is disabled.
  2786.                                      Positive, non-zero integers result in
  2787.                                      tracing information being written to
  2788.                                      'trace-file'. Higher settings of
  2789.                                      'trace-level' result in more trace
  2790.                                      information.
  2791.  
  2792.      upgrade-verbose     1 or 0      If 0, suppresses most output when
  2793.                                      upgrading packages. If 1, prints the
  2794.                                      name of each file as it is upgraded.
  2795.  
  2796.     For information about migrating options used by previous versions of
  2797.     PPM, see 'help ppm_migration'.
  2798.  
  2799.     When you assign a value to a setting, PPM saves the configuration.
  2800.     Therefore, setting values persist across sessions.
  2801. END
  2802. sub comp_settings {
  2803.     my $o = shift;
  2804.     my ($word, $line, $start) = @_;
  2805.     my @words = $o->line_parsed($line);
  2806.  
  2807.     # To please the users of Bash, we'll allow 'set foo=bar' to work as well,
  2808.     # since it's really easy to do:
  2809.     if (defined $words[1] and $words[1] =~ /=/ and not defined $words[2]) {
  2810.     my @kv = split '=', $words[1];
  2811.     splice(@words, 1, 1, @kv);
  2812.     }
  2813.     my $words = @words;
  2814.     my @compls;
  2815.  
  2816.     # return the keys when we're completing the second word
  2817.     if ($words == 1 or $words == 2 and $start != length($line)) {
  2818.     @compls = $o->settings_getkeys();
  2819.     return $o->completions($word, \@compls);
  2820.     }
  2821.  
  2822.     # Return no completions for 'unset'.
  2823.     return () if matches($o->{API}{cmd}{run}{name}, 'u|nset');
  2824.  
  2825.     # provide intelligent completion for arguments:
  2826.     if ($words ==2 or $words == 3 and $start != length($line)) {
  2827.     # Completion for boolean values:
  2828.     my @bool = $o->completions($words[1], \@boolean_keys);
  2829.     my @path = $o->completions($words[1], \@path_keys);
  2830.     if (@bool == 1) {
  2831.         return $o->completions($word, [0, 1]);
  2832.     }
  2833.     elsif (@path == 1) {
  2834.         @compls = readline::rl_filename_list($word);
  2835.         return $o->completions($word, \@compls);
  2836.     }
  2837.     elsif (matches($words[1], 's|ort-field')) {
  2838.         @compls = sort_fields();
  2839.         return $o->completions(lc($word), \@compls);
  2840.     }
  2841.     }
  2842.  
  2843.     # Don't complete for anything else.
  2844.     ()
  2845. }
  2846. sub run_settings {
  2847.     my $o = shift;
  2848.     my @args = @_;
  2849.     my $key = $args[0];
  2850.     my $val = $args[1];
  2851.  
  2852.     # To please the users of Bash, we'll allow 'set foo=bar' to work as well,
  2853.     # since it's really easy to do:
  2854.     if (defined $key and $key =~ /=/ and not defined $val) {
  2855.     ($key, $val) = split '=', $key;
  2856.     }
  2857.  
  2858.     trace(1, "PPM: settings @args\n");
  2859.     my $unset = matches($o->{API}{cmd}{run}{name}, 'u|nset');
  2860.     my @stuff = $o->completions($key, [$o->settings_getkeys()])
  2861.       if $key;
  2862.     my $fullkey = $stuff[0] if @stuff == 1;
  2863.     if (defined $key and defined $val) {
  2864.     # validate the key:
  2865.     unless ($fullkey) {
  2866.         $key = '' unless defined $key;
  2867.         $o->warn("Unknown or ambiguous setting '$key'. See 'help settings'.\n");
  2868.         return;
  2869.     }
  2870.     $o->conf($fullkey, $val, $unset);
  2871.     }
  2872.     elsif (defined $key) {
  2873.     unless ($fullkey) {
  2874.         $key = '' unless defined $key;
  2875.         $o->warn("Unknown or ambiguous setting '$key'. See 'help settings'.\n");
  2876.         return;
  2877.     }
  2878.     if ($unset) {
  2879.         $o->conf($fullkey, '', $unset);
  2880.     }
  2881.     else {
  2882.         my $val = $o->conf($fullkey);
  2883.         $o->print_pairs([$fullkey], [$val]);
  2884.     }
  2885.     }
  2886.     else {
  2887.     my (@keys, @vals);
  2888.     @keys = $o->settings_getkeys();
  2889.     @vals = $o->settings_getvals();
  2890.     my %k;
  2891.     @k{@keys} = @vals;
  2892.     @keys = sort keys %k;
  2893.     @vals = map { $k{$_} } @keys;
  2894.     $o->print_pairs(\@keys, \@vals);
  2895.     }
  2896. }
  2897. sub alias_settings { qw(unset) }
  2898.  
  2899. sub help_help { <<'END' }
  2900. help -- General help, or help on specific commands.
  2901.   Synopsis
  2902.  
  2903.      help                Lists available commands and help topics
  2904.      help 'command'      Lists detailed help about a specific command
  2905.  
  2906.   Description
  2907.  
  2908.     The help command provides a brief description of the commands available
  2909.     within PPM. For help on a specific command, enter help followed by the
  2910.     command name. For example, enter help settings or help set for a
  2911.     detailed description of the settings command.
  2912.  
  2913.     There are some extra help topics built into PPM. They can be accessed
  2914.     within the PPM environment as follows:
  2915.  
  2916.       help ppm_migration
  2917.  
  2918.     shows more details about the changes from previous versions of PPM
  2919.  
  2920.       help quickstart
  2921.  
  2922.     an easy-to-follow guide to getting started with PPM
  2923.  
  2924.       help prompt
  2925.  
  2926.     provides a detailed explanation about the PPM prompt
  2927. END
  2928.  
  2929. #============================================================================
  2930. # Version:
  2931. #============================================================================
  2932. sub smry_version { "displays the PPM version ($VERSION)" }
  2933. sub help_version { <<'END' }
  2934. version -- print the name and version of PPM.
  2935.     Prints the name and version of PPM3.
  2936. END
  2937. sub comp_version {()}
  2938. sub run_version {
  2939.     my $o = shift;
  2940.     if ($o->mode eq 'SHELL') {
  2941.     $o->inform("$NAME version $VERSION\n");
  2942.     }
  2943.     else {
  2944.     $o->inform("$SHORT_NAME $VERSION\n");
  2945.     }
  2946.     1;
  2947. }
  2948.  
  2949. #============================================================================
  2950. # Exit:
  2951. #============================================================================
  2952. sub help_exit { <<'END' }
  2953. exit, q, quit -- Exit the program
  2954.   Synopsis
  2955.  
  2956.      exit                Exit
  2957.      quit                Exit
  2958.      q                   Exit
  2959.      q <query>           Perform a new query (shortcut for query)
  2960.  
  2961.   Description
  2962.  
  2963.     When you leave the PPM environment, the current settings are saved.
  2964. END
  2965. sub comp_exit {
  2966.     my $o = shift;
  2967.     return &comp_query
  2968.     if $o->{API}{cmd}{run}{name} eq 'q' and @_;
  2969.     ();
  2970. }
  2971. sub run_exit {
  2972.     my $o = shift;
  2973.     # Special case: 'q' with no arguments should mean 'quit', but 'q' with
  2974.     # arguments should mean 'query'.
  2975.     if ($o->{API}{cmd}{run}{name} eq 'q' and @_) {
  2976.     return $o->run('query', @_);
  2977.     }
  2978.     $o->stoploop;
  2979. }
  2980. sub alias_exit { qw(quit q) }
  2981.  
  2982. #============================================================================
  2983. # Upgrade
  2984. # upgrade    # lists upgrades available
  2985. # upgrade <\d+> # upgrades specified package
  2986. # upgrade<pkg>    # upgrades named package
  2987. #============================================================================
  2988. sub smry_upgrade { "shows availables upgrades for installed packages" }
  2989. sub help_upgrade { <<'END' }
  2990. upgrade -- List or install available upgrades
  2991.   Synopsis
  2992.  
  2993.      upgrade [*]         Lists upgrades available for all installed packages
  2994.      upgrade <number>    Upgrades installed package <number>
  2995.      upgrade <range>     Upgrades a <range> of installed packages
  2996.      upgrade <package>   Upgrades the named <package>
  2997.  
  2998.   Description
  2999.  
  3000.     The upgrade command lists package upgrades that are available on the
  3001.     current repository for packages installed on your system. To install
  3002.     available upgrades, use the '--install' option.
  3003.  
  3004.     If profile tracking is enabled, (see 'help profile'), your profile will
  3005.     be updated to reflect changes to any packages which are upgraded.
  3006.  
  3007.     There are several modifiers to the upgrade command:
  3008.  
  3009.     -install
  3010.         Installs, rather than lists, available upgrades
  3011.  
  3012.     -precious
  3013.         Allows upgrading of "precious" packages
  3014.  
  3015.     -force
  3016.         See 'help install'
  3017.  
  3018.     -follow
  3019.         See 'help install'
  3020.  
  3021.     By default, 'upgrade' typed by itself only lists the available upgrades.
  3022.     To actually install all available upgrades, enter
  3023.  
  3024.         upgrade -install
  3025.  
  3026.     To enable upgrading "precious" packages, enter
  3027.  
  3028.         upgrade -install -precious
  3029.  
  3030.   See Also
  3031.  
  3032.     profile
  3033. END
  3034. sub comp_upgrade { goto &comp_properties; }
  3035. sub run_upgrade {
  3036.     my $o = shift;
  3037.     my @args = @_;
  3038.     trace(1, "PPM: upgrade @args\n");
  3039.  
  3040.     # Get options:
  3041.     my %opts = (
  3042.     install => 0,
  3043.     doprecious => 0,
  3044.     dryrun => 0,
  3045.     force => $o->conf('force-install'),
  3046.     follow => $o->conf('follow-install'),
  3047.     );
  3048.     {
  3049.     local @ARGV = @args;
  3050.     GetOptions(install => \$opts{install},
  3051.            precious => \$opts{doprecious},
  3052.            'force!' => \$opts{force},
  3053.            'follow!' => \$opts{follow},
  3054.            dryrun => \$opts{dryrun},
  3055.           );
  3056.     @args = @ARGV;
  3057.     }
  3058.  
  3059.     my $rlist = [$o->reps_on];
  3060.     my $targ  = $o->conf('target');
  3061.     my @pkgs;
  3062.  
  3063.     # Allow 'upgrade *';
  3064.     @args = grep { $_ ne '*' } @args;
  3065.  
  3066.     # List upgrades for a particular package
  3067.     if (@args) {
  3068.     my $pkg = $args[0];
  3069.     my @n = parse_range($o->raw_args);
  3070.     for my $n (@n) {
  3071.         my $ppd = $o->cache_entry('query', $n-1);
  3072.         unless($ppd) {
  3073.         $o->warn("No such query result '$pkg' in result set.\n");
  3074.         return;
  3075.         }
  3076.         else {
  3077.         push @pkgs, $ppd;
  3078.         }
  3079.     }
  3080.  
  3081.     # The name of the package:
  3082.     unless (@n) {
  3083.         my $ppd = PPM::UI::properties($o->conf('target'), $pkg);
  3084.         unless ($ppd->is_success) {
  3085.         $o->warn($ppd->msg);
  3086.         return unless $ppd->ok;
  3087.         }
  3088.         my $real_ppd = ($ppd->result_l)[0];
  3089.         push @pkgs, $real_ppd;
  3090.     }
  3091.     }
  3092.     # List upgrades for all packages
  3093.     else {
  3094.     @pkgs = PPM::UI::query($targ, '*', 0)->result_l;
  3095.     @pkgs = $o->sort_pkgs($o->conf('sort-field'), @pkgs);
  3096.     }
  3097.  
  3098.     for my $pkg (@pkgs) {
  3099.     my $name = $pkg->name;
  3100.     my $ver  = $pkg->getppd_obj->result->version;
  3101.     my $ok = PPM::UI::verify($rlist, $targ, $name);
  3102.  
  3103.     unless ($ok->is_success) {
  3104.         $o->warn("$name $ver: ", $ok->msg_raw, "\n");
  3105.         next unless $ok->ok;
  3106.     }
  3107.  
  3108.     my ($uptodate, $bundled, $precious, $newv, $oldv) = $ok->result_l;
  3109.  
  3110.     my $upgrade = 0;
  3111.     if ($uptodate) {
  3112.         $o->inform("$name $oldv: up to date.\n");
  3113.         $upgrade = $opts{install} && $opts{force};
  3114.     }
  3115.     else {
  3116.         $o->inform("$name $oldv: version $newv available.\n");
  3117.         $upgrade = $opts{install};
  3118.     }
  3119.     if ($upgrade) {
  3120.         if (not $precious or $precious && $opts{doprecious}) {
  3121.         $o->upgrade_pkg($name, \%opts);
  3122.         }
  3123.         else {
  3124.         $o->warn(<<END);
  3125. Use '-precious' to force precious packages to be upgraded.
  3126. END
  3127.         }
  3128.     }
  3129.     }
  3130.     1;
  3131. }
  3132.  
  3133. #============================================================================
  3134. # Profile:
  3135. # profile        # lists the profiles available on the repository
  3136. # profile N        # switches profiles
  3137. # profile add "name"    # adds a new profile
  3138. # profile delete N    # deletes the given profile
  3139. # profile describe N    # describes the given profile
  3140. # profile save        # saves the current state to the current profile
  3141. # profile restore    # restores the current profile
  3142. # profile rename    # renames the given profile
  3143. #============================================================================
  3144. sub smry_profiles { "manage PPM profiles" }
  3145. sub help_profiles { <<'END' }
  3146. profile -- Manage PPM Profiles
  3147.   Synopsis
  3148.  
  3149.      profile                     Lists profiles available on the repository
  3150.      profile <num>               Switches to the given profile
  3151.      profile add <name>          Creates a new profile on the repository
  3152.      profile delete <name or num>
  3153.                                  Deletes the given profile
  3154.      profile describe [name or num]
  3155.                                  Describes the current or given profile
  3156.      profile save                Saves the client state to the current profile
  3157.      profile restore             Restores the current profile
  3158.      profile rename <name or num> <name>
  3159.                                  Renames the given profile to <name>
  3160.  
  3161.   Description
  3162.  
  3163.     Profiles store information about packages that are installed on your
  3164.     system. If the 'profile-track' setting is enabled, your ASPN Profile
  3165.     will be updated with information about installed packages. Profiles
  3166.     allow you to easily migrate, reinstall, upgrade or restore PPM packages
  3167.     in one or more locations.
  3168.  
  3169.     To use profiles, you must have a license for ASPN. For license
  3170.     information, see http://www.ActiveState.com/ASPN/About. Disable profile
  3171.     tracking by setting 'profile-track=0'.
  3172. END
  3173. sub comp_profiles {
  3174.     my $o = shift;
  3175.     my ($word, $line, $start) = @_;
  3176.     my @words = $o->line_parsed($line);
  3177.     my $words = scalar @words;
  3178.     my @profs = PPM::UI::profile_list();
  3179.     my @cmds = ('add', 'delete', 'describe', 'save', 'restore', 'rename');
  3180.  
  3181.     if ($words == 1 or $words == 2 and $start != length($line)) {
  3182.     my @compls = (@cmds, 1 .. scalar @profs);
  3183.     return $o->completions($word, \@compls);
  3184.     }
  3185.     if ($words == 2 or $words == 3 and $start != length($line)) {
  3186.     return ()
  3187.       if ($o->completions($words[1], [qw(add save restore)])==1);
  3188.     return $o->completions($word, [1 .. scalar @profs])
  3189.       if ($o->completions($words[1], [qw(delete describe rename)])==1);
  3190.     }
  3191.     ();
  3192. }
  3193. sub run_profiles {
  3194.     my $o = shift;
  3195.     my @args = @_;
  3196.     trace(1, "PPM: profile @args\n");
  3197.  
  3198.     my $ok = PPM::UI::profile_list();
  3199.     unless ($ok->is_success) {
  3200.     $o->warn($ok->msg);
  3201.     return unless $ok->ok;
  3202.     }
  3203.     my @profiles = dictsort $ok->result_l;
  3204.     $ok = PPM::UI::profile_get();
  3205.     unless ($ok->is_success) {
  3206.     $o->warn($ok->msg);
  3207.     return unless $ok->ok;
  3208.     }
  3209.     my $profile = $ok->result;
  3210.     my $which = find_index($profile, 0, @profiles);
  3211.     if ($which < 0 and @profiles) {
  3212.     $profile = $profiles[0];
  3213.     PPM::UI::profile_set($profile);
  3214.     }
  3215.  
  3216.     if (@args) {
  3217.     # Switch to profile N:
  3218.     if ($args[0] =~ /^\d+$/) {
  3219.         my $num = $args[0];
  3220.         if (bounded(1, $num, scalar @profiles)) {
  3221.         my $profile = $profiles[$num-1];
  3222.         PPM::UI::profile_set($profile);
  3223.         }
  3224.         else {
  3225.         $o->warn("No such profile number '$num'.\n");
  3226.         return;
  3227.         }
  3228.     }
  3229.  
  3230.     # Describe profile N:
  3231.     elsif (matches($args[0], "des|cribe")) {
  3232.         my $num =     $args[1] =~ /^\d+$/ ? $args[1] :
  3233.             do {
  3234.                 my $n = find_index($args[1], 1, @profiles);
  3235.                 if ($n < 1) {
  3236.                 $o->warn("No such profile '$args[1]'.\n");
  3237.                 return;
  3238.                 }
  3239.                 $n;
  3240.             } if defined $args[1];
  3241.         my $prof;
  3242.         if (defined $num and $num =~ /^\d+$/) {
  3243.         if (bounded(1, $num, scalar @profiles)) {
  3244.             $prof = $profiles[$num - 1];
  3245.         }
  3246.         else {
  3247.             $o->warn("No such profile number '$num'.\n");
  3248.             return;
  3249.         }
  3250.         }
  3251.         elsif (defined $num) {
  3252.         $o->warn("Argument to '$args[0]' must be numeric; see 'help profile'.\n");
  3253.         return;
  3254.         }
  3255.         else {
  3256.         $prof = $profile;
  3257.         }
  3258.  
  3259.         my $res = PPM::UI::profile_info($prof);
  3260.         $o->warn($res->msg) and return unless $res->ok;
  3261.         my @res = $res->result_l;
  3262.         {
  3263.         my ($pkg, $version, $target);
  3264.         my $picture = <<'END';
  3265. [[[[[[[[[[[[[[[[[[[    [[[[[[[[[[[    [[[[[[[[[[[[[[[[[[[[[[
  3266. END
  3267.         ($pkg, $version, $target) = qw(PACKAGE VERSION TARGET);
  3268.         my $text = '';
  3269.         $text .= form($picture, $pkg, $version, $target)
  3270.           if @res;
  3271.         for my $entity (@res) {
  3272.             ($pkg, $version, $target) = @$entity;
  3273.             $version = "[$version]";
  3274.             $text .= form($picture, $pkg, $version, $target);
  3275.         }
  3276.         if (@res) {
  3277.             $o->inform("Describing Profile '$prof':\n");
  3278.         }
  3279.         else {
  3280.             $o->inform("Profile '$prof' is empty.\n");
  3281.         }
  3282.         $o->page($text);
  3283.         }
  3284.         return 1;
  3285.     }
  3286.  
  3287.     # Add a profile "name":
  3288.     elsif (matches($args[0], "a|dd")) {
  3289.         my $name = $args[1];
  3290.         if ($name) {
  3291.         # Note: do some heavy-duty error-checking; XXX
  3292.         PPM::UI::profile_add($name);
  3293.         PPM::UI::profile_save($name)
  3294.           if $o->conf('profile-track');
  3295.         PPM::UI::profile_set($name)
  3296.           unless $which >= 0;
  3297.         @profiles = PPM::UI::profile_list()->result_l;
  3298.         }
  3299.         else {
  3300.         $o->warn("Invalid use of 'add' command; see 'help profile'.\n");
  3301.         return;
  3302.         }
  3303.     }
  3304.  
  3305.     # Remove profile N:
  3306.     elsif (matches($args[0], "del|ete")) {
  3307.         my $num =    $args[1] =~ /^\d+$/ ? $args[1] :
  3308.             do {
  3309.                 my $n = find_index($args[1], 1, @profiles);
  3310.                 if ($n < 1) {
  3311.                 $o->inform("No such profile '$args[1]'.\n");
  3312.                 return;
  3313.                 }
  3314.                 $n;
  3315.             } if defined $args[1];
  3316.         if (defined $num and $num =~ /^\d+$/) {
  3317.         my $dead_profile = $profiles[$num-1];
  3318.         if (bounded(1, $num, scalar @profiles)) {
  3319.             PPM::UI::profile_del($dead_profile);
  3320.             @profiles = dictsort PPM::UI::profile_list()->result_l;
  3321.             if (@profiles and $dead_profile eq $profile) {
  3322.             $profile = $profiles[0];
  3323.             PPM::UI::profile_set($profile);
  3324.             }
  3325.             elsif (not @profiles) {
  3326.             $o->conf('profile-track', 0);
  3327.             PPM::UI::profile_set('');
  3328.             }
  3329.         }
  3330.         else {
  3331.             $o->warn("No such profile '$num'.\n");
  3332.             return;
  3333.         }
  3334.         }
  3335.         elsif (defined $num) {
  3336.         $o->warn(<<END);
  3337. Argument to '$args[0]' must be numeric; see 'help profile'.
  3338. END
  3339.         return;
  3340.         }
  3341.         else {
  3342.         $o->warn(<<END);
  3343. Invalid use of '$args[0]' command; see 'help profile'.
  3344. END
  3345.         return;
  3346. }
  3347.     }
  3348.  
  3349.     # Save current profile:
  3350.     elsif (matches($args[0], "s|ave")) {
  3351.         unless (@profiles) {
  3352.         $o->warn(<<END);
  3353. No profiles on the server. Use 'profile add' to add a profile.
  3354. END
  3355.         return;
  3356.         }
  3357.         unless ($which >= 0) {
  3358.         $o->warn(<<END);
  3359. No profile selected. Use 'profile <number>' to select a profile.
  3360. END
  3361.         return;
  3362.         }
  3363.         my $ok = PPM::UI::profile_save($profile);
  3364.         if ($ok->ok) {
  3365.         $o->inform("Profile '$profile' saved.\n");
  3366.         }
  3367.         else {
  3368.         $o->warn($ok->msg);
  3369.         return;
  3370.         }
  3371.         return 1;
  3372.     }
  3373.  
  3374.     # Rename profile:
  3375.     elsif (matches($args[0], "ren|ame")) {
  3376.         unless (@profiles) {
  3377.         $o->warn(<<END);
  3378. No profiles on the server. Use 'profile add' to add a profile.
  3379. END
  3380.         return;
  3381.         }
  3382.  
  3383.         # Determine the old name:
  3384.         my $num =    $args[1] =~ /^\d+$/ ? $args[1] :
  3385.             do {
  3386.                 my $n = find_index($args[1], 1, @profiles);
  3387.                 if ($n < 1) {
  3388.                 $o->warn("No such profile '$args[1]'.\n");
  3389.                 return;
  3390.                 };
  3391.                 $n;
  3392.             } if defined $args[1];
  3393.         my $oldprof;
  3394.         if (defined $num and $num =~ /^\d+$/) {
  3395.         if (bounded(1, $num, scalar @profiles)) {
  3396.             $oldprof = $profiles[$num - 1];
  3397.         }
  3398.         else {
  3399.             $o->warn("No such profile number '$num'.\n");
  3400.             return;
  3401.         }
  3402.         }
  3403.         elsif (defined $num) {
  3404.         $o->warn("Argument to '$args[0]' must be numeric; see 'help profile'.\n");
  3405.         return;
  3406.         }
  3407.         else {
  3408.         $o->warn("profile: invalid use of '$args[0]' command: see 'help profile'.\n");
  3409.         return;
  3410.         }
  3411.  
  3412.         # Validate the new name:
  3413.         my $newprof = $args[2];
  3414.         unless (defined $newprof and length($newprof)) {
  3415.         $newprof = '' unless defined $newprof;
  3416.         $o->warn(<<END);
  3417. Profile names must be non-empty: '$newprof' is not a valid name.
  3418. END
  3419.         return;
  3420.         }
  3421.  
  3422.         # Actually do it:
  3423.         my $ok = PPM::UI::profile_rename($oldprof, $newprof);
  3424.         unless ($ok->is_success) {
  3425.         $o->warn($ok->msg);
  3426.         return unless $ok->ok;
  3427.         }
  3428.         if ($profile eq $oldprof) {
  3429.         $profile = $newprof;
  3430.         PPM::UI::profile_set($profile);
  3431.         }
  3432.         @profiles = dictsort PPM::UI::profile_list()->result_l;
  3433.     }
  3434.  
  3435.     # Restore current profile:
  3436.     elsif (matches($args[0], "res|tore")) {
  3437.         unless (@profiles) {
  3438.         $o->warn(<<END);
  3439. No profiles on this server. Use 'profile add' to add a profile.
  3440. END
  3441.         return;
  3442.         }
  3443.         unless ($which >= 0) {
  3444.         $o->warn(<<END);
  3445. No profile selected. Use 'profile <number>' to select a profile.
  3446. END
  3447.         return;
  3448.         }
  3449.         my ($clean_packages, $dry) = (0, 0);
  3450.         my ($force, $follow) = (1, 0);
  3451.         {
  3452.         local @ARGV = @args;
  3453.         GetOptions('clean!' => \$clean_packages,
  3454.                'force!' => \$force,
  3455.                'follow!' => \$follow,
  3456.                'dryrun' => \$dry,
  3457.               );
  3458.         @args = @ARGV;
  3459.         }
  3460.         my $cb_inst = $dry ? \&dr_install : \&cb_install;
  3461.         my $cb_rm   = $dry ? \&dr_remove  : \&cb_remove ;
  3462.         my $ok = PPM::UI::profile_restore($profile, $cb_inst,
  3463.                           $cb_rm, $force, $follow,
  3464.                           $dry, $clean_packages);
  3465.         if ($ok->ok) {
  3466.         $o->cache_clear('query');
  3467.         $o->inform("Profile '$profile' restored.\n");
  3468.         }
  3469.         else {
  3470.         $o->warn($ok->msg);
  3471.         return;
  3472.         }
  3473.         return 1;
  3474.     }
  3475.  
  3476.     # Unrecognized subcommand:
  3477.     else {
  3478.         $o->warn("No such profile command '$args[0]'; see 'help profile'.\n");
  3479.         return;
  3480.     }
  3481.     }
  3482.     if (@profiles) {
  3483.     @profiles = dictsort @profiles;
  3484.     my $i = 0;
  3485.     $o->inform("Profiles:\n");
  3486.     my $profile = PPM::UI::profile_get()->result;
  3487.     for (@profiles) {
  3488.         $o->informf("%s%2d", $profile eq $profiles[$i] ? "*" : " ", $i + 1);
  3489.         $o->inform(". $_\n");
  3490.         $i++;
  3491.     }
  3492.     }
  3493.     elsif (defined $args[0] and matches($args[0], "del|ete")) {
  3494.     # assume that we just deleted the last profile
  3495.     $o->warn(<<END);
  3496. Profile deleted; no remaining profiles on the server.
  3497. END
  3498.     }
  3499.     else {
  3500.     $o->warn(<<END);
  3501. No profiles. Use 'profile add' to add a profile.
  3502. END
  3503.     }
  3504.     1;
  3505. }
  3506.  
  3507. #============================================================================
  3508. # Help-only topics:
  3509. #============================================================================
  3510. sub smry_prompt { "how to interpret the PPM prompt" }
  3511. sub help_prompt { <<'END' }
  3512. prompt -- information about the PPM3 prompt
  3513.   Description
  3514.  
  3515.     The PPM prompt can tell you six things:
  3516.  
  3517.     1)  The current repository;
  3518.  
  3519.     2)  The current target;
  3520.  
  3521.     3)  The last search you made on the current repository;
  3522.  
  3523.     4)  The last query you made on the current target;
  3524.  
  3525.     5)  The last package you described from this repository; and,
  3526.  
  3527.     6)  The last package you described from this target.
  3528.  
  3529.     To enable the prompt to tell you this information, you must set
  3530.     'prompt-context' to '1'. The following examples all assume this setting.
  3531.  
  3532.   Examples
  3533.  
  3534.     1   Repository and Target:
  3535.  
  3536.         Set 'prompt-context' The prompt will resemble:
  3537.  
  3538.             ppm:1:1> 
  3539.  
  3540.         In this case, the first '1' means that the first repository is
  3541.         selected. The second '1' means the first target is selected. You can
  3542.         prove this by adding another repository and switching to it:
  3543.  
  3544.             ppm:1:1> rep add TEMP http://my/repository
  3545.             Repositories:
  3546.               1. ActiveState Package Repository
  3547.             * 2. TEMP
  3548.             ppm:1:1> rep 2
  3549.             Repositories:
  3550.               1. ActiveState Package Repository
  3551.             * 2. TEMP
  3552.             ppm:2:1> 
  3553.  
  3554.         The same is true for targets. If you have multiple versions of Perl
  3555.         installed, when you swtich to a different target the second number
  3556.         reflects the change.
  3557.  
  3558.         If you delete all the repositories, the repository number changes to
  3559.         '?'. The same goes for targets. If either item is indicated by a
  3560.         question mark, you must configure a repository or target before
  3561.         proceeding.
  3562.  
  3563.     2   Search and Query:
  3564.  
  3565.         PPM stores searches and search results from in the current session.
  3566.         The prompt displays the search number:
  3567.  
  3568.             ppm:1:1> search Text
  3569.             [results displayed here]
  3570.             ppm:1:1:s1> 
  3571.  
  3572.         The 's1' indicates that the last search you performed can be viewed
  3573.         again by entering 'search 1'. Type 'search' with no arguments to
  3574.         view the list of cached searches:
  3575.  
  3576.             ppm:1:1:s1> search
  3577.             Search Result Sets:
  3578.             * 1. Text
  3579.  
  3580.         If you then enter 'search 1', you will see the same results as when
  3581.         you typed 'search Text' earlier. If you search for something else
  3582.         ('search Parse') then the number will change to 's2':
  3583.  
  3584.             ppm:1:1:s1> search Parse
  3585.             [results displayed here]
  3586.             ppm:1:1:s2>
  3587.  
  3588.         The same indicators apply to the query command. When you run a
  3589.         query, a numerical indicator displays the current query:
  3590.  
  3591.             ppm:1:1:s1> query PPM
  3592.             [results displayed here]
  3593.             ppm:1:1:s1:q1> 
  3594.  
  3595.         You can view the past queries with 'query', and view results by
  3596.         querying a particular number.
  3597.  
  3598.     3   Describe and Properties:
  3599.  
  3600.         When you use the describe command with the numerical switch (to view
  3601.         package information based on the package number in the last search
  3602.         or query), PPM sets that index to the current index. If you use the
  3603.         desribe command with the name switch, and the name is found within
  3604.         the current result, the index is set to the current one. If no
  3605.         package is found, PPM creates a new search or query on-the-fly, and
  3606.         sets it as the current search or query.
  3607.  
  3608.         For example:
  3609.  
  3610.             ppm:1:1> search Text
  3611.             1. Convert-Context  [0.501]     an Attributed Text data type
  3612.             2. gettext          [1.01]      message handling functions
  3613.             3. HTML-FromText    [1.005]     mark up text as HTML
  3614.             4. HTML-Subtext     [1.03]      Perform text substitutions on an HTML
  3615.                                             template
  3616.             5. Locale-Maketext  [0.18]      framework for software localization
  3617.             ppm:1:1:s1>
  3618.  
  3619.             ppm:1:1:s1> describe 1
  3620.             ====================
  3621.             Package 1:
  3622.                 Name: Convert-Context
  3623.              Version: 0.501
  3624.               Author: Martin Schwartz (martin@nacho.de)
  3625.             Abstract: an Attributed Text data type
  3626.             Implementations:
  3627.                    1. i686-linux-thread-multi
  3628.                    2. MSWin32-x86-multi-thread
  3629.                    3. sun4-solaris-thread-multi
  3630.             ====================
  3631.             ppm:1:1:s1:sp1> 
  3632.  
  3633.         The last prompt has an extra 'sp1'. That stands for 'search package
  3634.         1', and it means that PPM considers 'Convert-Context' to be the
  3635.         default package. If you now type 'describe' or 'install' with no
  3636.         arguments, PPM will apply your command to this package.
  3637.  
  3638.         If you go back to where you had no default package selected:
  3639.  
  3640.             ppm:1:1> search Text
  3641.             1. Convert-Context  [0.501]     an Attributed Text data type
  3642.             2. gettext          [1.01]      message handling functions
  3643.             3. HTML-FromText    [1.005]     mark up text as HTML
  3644.             4. HTML-Subtext     [1.03]      Perform text substitutions on an HTML
  3645.                                             template
  3646.             5. Locale-Maketext  [0.18]      framework for software localization
  3647.             ppm:1:1:s1>
  3648.  
  3649.         ...and you describe 'Locale-Maketext', you will see this:
  3650.  
  3651.             ppm:1:1:s1> describe Locale-Maketext
  3652.             ====================
  3653.                 Name: Locale-Maketext
  3654.              Version: 0.18
  3655.               Author: Sean M. Burke (sburke@cpan.org)
  3656.             Abstract: framework for software localization
  3657.             Prerequisites:
  3658.                    1. I18N-LangTags 0.13
  3659.             Implementations:
  3660.                    1. i686-linux-thread-multi
  3661.                    2. MSWin32-x86-multi-thread
  3662.                    3. sun4-solaris-thread-multi
  3663.             ====================
  3664.             ppm:1:1:s1:sp5>
  3665.  
  3666.         Notice that the correct package got selected, even though you
  3667.         specified it by name.
  3668.  
  3669.     This behaviour also applies to the query and properties commands.
  3670.  
  3671.   See Also
  3672.  
  3673.     describe, properties, query, search
  3674. END
  3675.  
  3676. sub run_quickstart  { $_[0]->help('quickstart') }
  3677. sub smry_quickstart { "a crash course in using PPM" }
  3678. sub help_quickstart { <<'END' }
  3679. quickstart -- a beginners' guide to PPM3
  3680.   Description
  3681.  
  3682.     PPM (Programmer's Package Manager) is a utility for managing software
  3683.     "packages". A package is a modular extension for a language or a
  3684.     software program. Packages reside in repositories. PPM can use three
  3685.     types of repositories:
  3686.  
  3687.      1) A directory on a CD-ROM or hard drive in your computer
  3688.      2) A website
  3689.      3) A remote Repository Server (such as ASPN)
  3690.  
  3691.     Common Commands:
  3692.  
  3693.     To view PPM help:
  3694.  
  3695.       help
  3696.       help <command>
  3697.  
  3698.     To view the name of the current repository:
  3699.  
  3700.       repository
  3701.  
  3702.     To search the current repository:
  3703.  
  3704.       search <keywords>
  3705.  
  3706.     To install a package:
  3707.  
  3708.       install <package_name>
  3709.  
  3710.     Most commands can be truncated; as long as the command is unambiguous,
  3711.     PPM will recognize it. For example, 'repository add foo" can be entered
  3712.     as 'rep add foo'.
  3713.  
  3714.     PPM features user profiles, which store information about installed
  3715.     packages. Profiles are stored as part of your ASPN account; thus, you
  3716.     can easily maintain package profiles for different languages, or
  3717.     configure one machine with your favorite packages, and then copy that
  3718.     installation to another machine by accessing your ASPN profile.
  3719.  
  3720.     For more information, type 'help profile' at the PPM prompt.
  3721. END
  3722.  
  3723. sub smry_ppm_migration { "guide for those familiar with PPM" }
  3724. sub help_ppm_migration { <<'END' }
  3725. ppm migration -- PPM Migration Guide
  3726.   Description
  3727.  
  3728.     Those familiar with PPM version 2 should appreciate the extended
  3729.     functionality of PPM version 3, including the command-line history,
  3730.     autocomplete and profiles. Some PPM version 2 commands are different in
  3731.     PPM version 3. Examples of command changes include:
  3732.  
  3733.     1   Adding a repository
  3734.  
  3735.         PPM2:
  3736.  
  3737.           set repository my_repository http://my/repository
  3738.  
  3739.         PPM3:
  3740.  
  3741.           repository add my_repository http://my/repository
  3742.  
  3743.     2   Removing a repository
  3744.  
  3745.         PPM2:
  3746.  
  3747.           set repository --remove my_repository
  3748.  
  3749.         PPM3:
  3750.  
  3751.           repository del my_repository
  3752.  
  3753.     3   Setting the temporary directory
  3754.  
  3755.         PPM2:
  3756.  
  3757.           set build DIRECTORY
  3758.  
  3759.         PPM3
  3760.  
  3761.           set tempdir DIRECTORY
  3762.  
  3763.     4   Setting frequency of download updates
  3764.  
  3765.         PPM2:
  3766.  
  3767.           set downloadstatus NUMBER
  3768.  
  3769.         PPM3:
  3770.  
  3771.           set download-chunksize NUMBER
  3772.  
  3773.     5   Changing the installation root directory:
  3774.  
  3775.         PPM2:
  3776.  
  3777.           set root DIRECTORY
  3778.  
  3779.         PPM3:
  3780.  
  3781.           target set root DIRECTORY
  3782.  
  3783.     6   Listing all installed packages:
  3784.  
  3785.         PPM2:
  3786.  
  3787.           query
  3788.  
  3789.         PPM3:
  3790.  
  3791.           query *
  3792.  
  3793.     7   Listing all packages on server:
  3794.  
  3795.         PPM2:
  3796.  
  3797.           search
  3798.  
  3799.         PPM3:
  3800.  
  3801.           search *
  3802. END
  3803.  
  3804. sub smry_unicode { "notes about unicode author names" }
  3805. sub help_unicode { <<'END' }
  3806. unicode -- Notes About Unicode Author Names
  3807.   Description
  3808.  
  3809.     CPAN author names are defined to be in Unicode. Unicode is an
  3810.     international standard ISO 10646, defining the *Universal Character Set
  3811.     (UCS)*. UCS contains all characters of all other character set
  3812.     standards. For more information about Unicode, see
  3813.     http://www.unicode.org/.
  3814.  
  3815.     The CPAN authors website is located at your local CPAN mirror under
  3816.     /authors/00whois.html. For example, you can view it at
  3817.     http://www.cpan.org/authors/00whois.html. This page can be rendered by
  3818.     Mozilla 0.9.8 and Internet Explorer 5.0, but you may have to install
  3819.     extra language packs to view all the author names.
  3820.  
  3821.     By default, PPM3 renders all characters as Latin1 when it prints them to
  3822.     your console. Characters outside the Latin1 range (0-255) are not
  3823.     printed at all.
  3824.  
  3825.     If your console can render UTF-8 characters, you can tell PPM3 not to
  3826.     recode characters by using one of the following environment variables:
  3827.  
  3828.         LC_ALL
  3829.  
  3830.         LC_CTYPE
  3831.  
  3832.         LANG
  3833.  
  3834.         PPM_LANG
  3835.  
  3836.     PPM3 requires one of these environment variables to contain the string
  3837.     'UTF-8'. For example, the following setting make PPM3 print
  3838.     beautifully-formatted authors in RedHat Linux 7.2 (assumes you're using
  3839.     a Bourne shell):
  3840.  
  3841.       $ PPM_LANG='en_US.UTF-8' xterm -u8 -e ppm3
  3842.  
  3843.     Linux and Solaris users should refer to the xterm manpage for more
  3844.     information about setting up xterm to display UTF-8 characters.
  3845. END
  3846.  
  3847. #============================================================================
  3848. # Utility Functions
  3849. #============================================================================
  3850. sub sort_fields { qw(name title author abstract version repository) }
  3851. sub sort_pkgs {
  3852.     my $o = shift;
  3853.     my $field = lc shift;
  3854.     my @pkgs = @_;
  3855.     my $targ = $o->conf('target');
  3856.     my $filt = sub { $_[0]->getppd_obj($targ)->result->$field };
  3857.     if ($field eq 'name') {
  3858.     return dictsort $filt, @pkgs;
  3859.     }
  3860.     if ($field eq 'title') {
  3861.     return dictsort $filt, @pkgs;
  3862.     }
  3863.     if ($field eq 'author') {
  3864.     return dictsort $filt, @pkgs;
  3865.     }
  3866.     if ($field eq 'abstract') {
  3867.     return dictsort $filt, @pkgs;
  3868.     }
  3869.     if ($field eq 'repository') {
  3870.     return dictsort sub { $_[0]->repository->name }, @pkgs;
  3871.     }
  3872.     if ($field eq 'version') {
  3873.     return sort {
  3874.         my $pa = $a->getppd_obj($targ)->result;
  3875.         my $pb = $b->getppd_obj($targ)->result;
  3876.         $pb->uptodate($pa->version_osd) <=> $pa->uptodate($pb->version_osd)
  3877.     } @pkgs;
  3878.     }
  3879.     @pkgs;
  3880. }
  3881.  
  3882. sub find_index {
  3883.     my $entry = shift || '';
  3884.     my $index = shift;
  3885.     $index = 0 unless defined $index;
  3886.     for (my $i=0; $i<@_; $i++) {
  3887.     return $index + $i if $entry eq $_[$i];
  3888.     }
  3889.     return $index - 1;
  3890. }
  3891.  
  3892. sub bounded {
  3893.     my $lb = shift;
  3894.     my $d = shift;
  3895.     my $ub = shift;
  3896.     return ($d >= $lb and $d <= $ub);
  3897. }
  3898.  
  3899. sub dictsort(@) {
  3900.     my $o = shift if eval { $_[0]->isa("PPMShell") };
  3901.     my $filt = ref($_[0]) eq 'CODE' ? shift @_ : undef;
  3902.     return map { $_->[0] }
  3903.        sort { lc $a->[1] cmp lc $b->[1] }
  3904.        map { [ $_, $filt ? $filt->($_) : $_ ] } @_;
  3905. }
  3906.  
  3907. sub path_under {
  3908.     my $path = shift;
  3909.     my $cmp  = shift;
  3910.     if ($^O eq 'MSWin32') {
  3911.     $path =~ s#\\#/#g;
  3912.     $cmp  =~ s#\\#/#g;
  3913.     return $path =~ /^\Q$cmp\E/i;
  3914.     }
  3915.     else {
  3916.     return $path =~ /^\Q$cmp\E/;
  3917.     }
  3918. }
  3919.  
  3920. sub prompt_str {
  3921.     my $o = shift;
  3922.  
  3923.     # Hack: set the pager here, instead of in settings_setkey()
  3924.     $o->{API}{pager} = $o->conf('pager');
  3925.  
  3926.     my @search_results = $o->cache_sets('search');
  3927.     my $search_result_current = $o->cache_set_current('search');
  3928.     my $search_result_index = $o->cache_set_index('search');
  3929.     my @query_results = $o->cache_sets('query');
  3930.     my $query_result_current = $o->cache_set_current('query');
  3931.     my $query_result_index = $o->cache_set_index('query');
  3932.  
  3933.     # Make sure a profile is selected if they turned tracking on.
  3934.     my $profile_track = $o->conf('profile-track');
  3935.     my $profile       = PPM::UI::profile_get()->result;
  3936.     $o->setup_profile()
  3937.     if $profile_track and not $profile and $o->mode eq 'SHELL';
  3938.  
  3939.     my @targs = PPM::UI::target_list()->result_l;
  3940.     if (@targs and not find_index($o->conf('target'), 1, @targs)) {
  3941.     $o->conf('target', $targs[0]);
  3942.     }
  3943.  
  3944.     if ($o->conf('prompt-context')) {
  3945.     my ($targ, $rep, $s, $sp, $q, $qp);
  3946.  
  3947.     if ($o->conf('prompt-verbose')) {
  3948.         my $sz = $o->conf('prompt-slotsize');
  3949.         $targ = substr($o->conf('target'), 0, $sz);
  3950.         $rep  = substr($o->conf('repository'), 0, $sz);
  3951.  
  3952.         my $sq_tmp = $o->cache_set('search', undef, 'query');
  3953.         my $ss_tmp = $o->cache_set('search');
  3954.         my $sp_tmp = $o->cache_entry('search');
  3955.         $s = (defined $sq_tmp)
  3956.           ? ":" . substr($sq_tmp, 0, $sz)
  3957.           : "";
  3958.         $sp = ($s and defined $sp_tmp and
  3959.            bounded(0, $search_result_index, $#$ss_tmp))
  3960.           ? ":" . substr($sp_tmp->name, 0, $sz)
  3961.           : "";
  3962.  
  3963.         my $qq_tmp = $o->cache_set('query', undef, 'query');
  3964.         my $qs_tmp = $o->cache_set('query');
  3965.         my $qp_tmp = $o->cache_entry('query');
  3966.         $q = (defined $qq_tmp)
  3967.           ? ":" . substr($qq_tmp, 0, $sz)
  3968.           : "";
  3969.         $qp = ($q and defined $qp_tmp and
  3970.            bounded(0, $query_result_index, $#$qs_tmp))
  3971.           ? ":" . substr($qp_tmp->name, 0, $sz)
  3972.           : "";
  3973.     }
  3974.     else {
  3975.         # Target and Repository:
  3976.         $targ = find_index($o->conf('target'), 1, @targs);
  3977.         $targ = '?' if $targ == 0;
  3978.     
  3979.         # Search number & package:
  3980.         $s = @search_results ? ":s".($search_result_current + 1) : "";
  3981.         my $sp_tmp = $o->cache_set('search');
  3982.         $sp = ($s and defined $sp_tmp and 
  3983.            bounded(0, $search_result_index, $#$sp_tmp))
  3984.           ? ":sp".($search_result_index + 1)
  3985.           : "";
  3986.     
  3987.         # Query number & package:
  3988.         $q = @query_results ? ":q".($query_result_current + 1) : "";
  3989.         my $qp_tmp = $o->cache_set('query');
  3990.         $qp = ($q and defined $qp_tmp and
  3991.            bounded(0, $query_result_index, $#$qp_tmp))
  3992.           ? ":qp".($query_result_index + 1)
  3993.           : "";
  3994.     }
  3995.     return "ppm:$targ$s$sp$q$qp> ";
  3996.     }
  3997.     else {
  3998.     return "ppm> ";
  3999.     }
  4000. }
  4001.  
  4002. {
  4003.     # Weights for particular fields: these are stored in percentage of the
  4004.     # screen width, based on the number of columns they use on an 80 column
  4005.     # terminal. They also have a minimum and maximum.
  4006.     use constant MIN    => 0;
  4007.     use constant MAX    => 1;
  4008.     my %weight = (
  4009.     name     => [12, 20],
  4010.     title    => [12, 20],
  4011.     abstract => [12, 20],
  4012.     author   => [12, 20],
  4013.     repository => [12, 20],
  4014.     version  => [ 4,  9],
  4015.     );
  4016.     my %meth = (
  4017.     name     => 'name',
  4018.     title    => 'title',
  4019.     version  => 'version',
  4020.     abstract => 'abstract',
  4021.     author   => 'author',
  4022.     repository => sub {
  4023.         my $o = shift;
  4024.         my $rep = $o->repository or return "Installed";
  4025.         my $name = $rep->name;
  4026.         my $id   = $o->id || $name;
  4027.         my $loc  = $rep->location;
  4028.         "$name [$loc]"
  4029.     },
  4030.     );
  4031.     # These are Text::Autoformat justification marks. They're actually used to
  4032.     # build a printf() format string, since it's so much more efficient for a
  4033.     # non-line-wrapping case.
  4034.     my %just = (
  4035.     name     => '<',
  4036.     title    => '<',
  4037.     abstract => '<',
  4038.     author   => '<',
  4039.     repository => '<',
  4040.     version  => '>',
  4041.     );
  4042.     my %plus = (
  4043.     name     => '0',
  4044.     title    => '0',
  4045.     abstract => '0',
  4046.     author   => '0',
  4047.     repository => '0',
  4048.     version  => '2',
  4049.     );
  4050.     my %filt = (
  4051.     version => q{"[$_]"},
  4052.     );
  4053.     sub picture_optimized {
  4054.     my $o = shift;
  4055.     my @items = @{shift(@_)};
  4056.     unless ($o->conf('fields')) {
  4057.         my $m = $o->setmode('SILENT');
  4058.         $o->conf('fields', '', 1);
  4059.         $o->setmode($m);
  4060.     }
  4061.     my @fields = split ' ', $o->conf('fields');
  4062.     $_ = lc $_ for @fields;
  4063.     my (%max_width, %width);
  4064.     my $cols = $o->termsize->{cols};
  4065.     for my $f (@fields) {
  4066.         my $meth = $meth{$f};
  4067.         $max_width{$f} = max { length($_->$meth) } @items;
  4068.         $max_width{$f} += $plus{$f};
  4069.         $width{$f} = $max_width{$f} / 80 * $cols;
  4070.         my $max_f  = $weight{$f}[MAX] / 80 * $cols;
  4071.         my $min_f  = $weight{$f}[MIN];
  4072.         my $gw     = $width{$f};
  4073.         $width{$f} = (
  4074.         $width{$f} > $max_width{$f} ? $max_width{$f} :
  4075.         $width{$f} > $max_f         ? $max_f         :
  4076.         $width{$f} < $min_f         ? $min_f         : $width{$f}
  4077.         );
  4078.     }
  4079.     my $right = $fields[-1];
  4080.     my $index_sz = length( scalar(@items) ) + 3; # index spaces
  4081.     my $space_sz = @fields + 1; # separator spaces
  4082.     my $room = $cols - $index_sz - $space_sz;
  4083.     $width{$right} = $room - sum { $width{$_} } @fields[0 .. $#fields-1];
  4084.     while ($width{$right} > $max_width{$right}) {
  4085.         my $smallest;
  4086.         my $n;
  4087.         for my $k (@fields[0 .. $#fields-1]) {
  4088.         my $max = $max_width{$k};
  4089.         my $sz  = $width{$k};
  4090.         $smallest = $k, $n = $max - $sz if $max - $sz > $n;
  4091.         }
  4092.         $width{$right}--;
  4093.         $width{$smallest}++;
  4094.     }
  4095.     while ($width{$right} < $weight{$right}[MIN]) {
  4096.         my $biggest;
  4097.         my $n;
  4098.         for my $k (@fields[0 .. $#fields-1]) {
  4099.         my $max = $max_width{$k};
  4100.         my $sz  = $width{$k};
  4101.         $biggest = $k, $n = $max - $sz if $max - $sz < $n;
  4102.         }
  4103.         $width{$right}++;
  4104.         $width{$biggest}--;
  4105.     }
  4106.     my $picture;
  4107.     $picture = "\%${index_sz}s "; # printf picture
  4108.     $picture .= join ' ', map {
  4109.         my $w = $width{$_};
  4110.         my $c = $just{$_};
  4111.         my $pad = $c eq '>' ? '' : '-';
  4112.         "\%${pad}${w}s" # printf picture
  4113.     } @fields;
  4114.     ($picture, \@fields, [@width{@fields}]);
  4115.     }
  4116.  
  4117.     sub print_formatted {
  4118.     my $o = shift;
  4119.     my $targ = $o->conf('target');
  4120.     my @items = map { $_->getppd_obj($targ)->result } @{shift(@_)};
  4121.     my $selected = shift;
  4122.     my $format;
  4123.  
  4124.     # Generate a picture and a list of fields for Text::Autoformat:
  4125.     my (@fields, %width);
  4126.     my ($picture, $f, $w) = $o->picture_optimized(\@items);
  4127.     $picture .= "\n";
  4128.     @fields = @$f;
  4129.     @width{@fields} = @$w;
  4130.  
  4131.     # The line-breaking sub: use '~' as hyphenation signal
  4132.     my $wrap = sub {
  4133.         my ($str, $maxlen, $width) = @_;
  4134.         my $field = substr($str, 0, $maxlen - 1) . '~';
  4135.         my $left  = substr($str, $maxlen - 1);
  4136.         ($field, $left);
  4137.     };
  4138.  
  4139.     my $lines = 0;
  4140.     my $i = 1;
  4141.     my @text;
  4142.     my %seen;
  4143.     for my $pkg (@items) {
  4144.         my $star = (defined $selected and $selected == $i - 1) ? "*" : " ";
  4145.         my $num  = "$star $i.";
  4146.         my @vals = (
  4147.         map {
  4148.             my $field  = $_;
  4149.             my $method = $meth{$field};
  4150.             local $_   = $pkg->$method;
  4151.             my $val = defined $filt{$field} ? eval $filt{$field} : $_;
  4152.             ($val) = $wrap->($val, $width{$field})
  4153.                 if length $val > $width{$field};
  4154.             $val;
  4155.         }
  4156.         @fields
  4157.         );
  4158. #        my $key = join '', @vals;
  4159. #        if (exists $seen{$key}) {
  4160. #        my $index = $seen{$key};
  4161. #        substr($text[$index], 0, 1) = '+';
  4162. #        next;
  4163. #        }
  4164. #        $seen{$key} = $i - 1;
  4165.         (my $inc = sprintf $picture, $num, @vals) =~ s/[ ]+$//;
  4166.         push @text, $inc;
  4167.         $i++;
  4168.     }
  4169.  
  4170.     # And, page it.
  4171.     $o->page(join '', @text);
  4172.     }
  4173. }
  4174.  
  4175. sub tree_pkg {
  4176.     my $o = shift;
  4177.     my @rlist = $o->reps_on;
  4178.     my $tar = $o->conf('target');
  4179.     my $pkg = shift;
  4180.     my $ppd;
  4181.     if (eval { $pkg->isa('PPM::Package') }) {
  4182.     $ppd = $pkg->getppd_obj($tar)->result;
  4183.     }
  4184.     else {
  4185.     my ($s, $i) = $o->cache_find('search', $pkg);
  4186.     if ($i >= 0) {
  4187.         $ppd = $o->cache_entry('search', $i, $s);
  4188.     } 
  4189.     else {
  4190.         my $ok = PPM::UI::describe(\@rlist, $tar, $pkg);
  4191.         unless ($ok->is_success) {
  4192.         $o->warn($ok->msg);
  4193.         return unless $ok->ok;
  4194.         }
  4195.         $ppd = $ok->result->getppd_obj($tar)->result;
  4196.     }
  4197.     }
  4198.  
  4199.     my $pad = "\n";
  4200.     $o->inform($ppd->name, " ", $ppd->version);
  4201.     $o->Tree(\@rlist, $tar, $ppd->name, $pad, {});
  4202.     $o->inform($pad);
  4203. }
  4204.  
  4205. my ($VER, $HOR, $COR, $TEE, $SIZ) = ('|', '_', '\\', '|', ' ');
  4206.  
  4207. sub Tree {
  4208.     my $o = shift;
  4209.     my $reps = shift;
  4210.     my $tar = shift;
  4211.     my $pkg = shift;
  4212.     my $ind = shift;
  4213.     my $seen = shift;
  4214.     my $pad = $ind . "  " . $VER;
  4215.  
  4216.     my $ppd;
  4217.     if (exists $seen->{$pkg}) {
  4218.     $ppd = $seen->{$pkg};
  4219.     }
  4220.     else {
  4221.     my ($s, $i) = $o->cache_find('search', $pkg);
  4222.     if ($i >= 0) {
  4223.         $ppd = $o->cache_entry('search', $i, $s);
  4224.     }
  4225.     else {
  4226.         my $ok = PPM::UI::describe($reps, $tar, $pkg);
  4227.         unless ($ok->is_success) {
  4228.         $o->inform(" -- package not found; skipping tree");
  4229.         return 0 unless $ok->ok;
  4230.         }
  4231.         $ppd = $ok->result;
  4232.     }
  4233.     $ppd->make_complete($tar);
  4234.     $ppd = $ppd->getppd_obj($tar)->result;
  4235.     $seen->{$pkg} = $ppd;
  4236.     }
  4237.  
  4238.     my @impls   = $ppd->implementations;
  4239.     return 0 unless @impls;
  4240.     my @prereqs = $impls[0]->prereqs;
  4241.     return 0 unless @prereqs;
  4242.     my $nums = scalar @prereqs;
  4243.  
  4244.     for (1..$nums) {
  4245.     my $doneblank = 0;
  4246.     my $pre = $prereqs[$_-1];
  4247.     my $txt = $pre->name . " " . $pre->version;
  4248.     if ($_ == $nums) {
  4249.         substr($pad, -1) = $COR;
  4250.         $o->inform($pad, "$HOR$HOR", $txt);
  4251.         substr($pad, -1) = ' ';
  4252.     }
  4253.     else {
  4254.         substr($pad, -1) = $TEE;
  4255.         $o->inform($pad, "$HOR$HOR", $txt);
  4256.         substr($pad, -1) = $VER;
  4257.     }
  4258.     if ($o->Tree($reps, $tar, $pre->name, $pad, $seen) != 0 and
  4259.         $doneblank == 0) {
  4260.         $o->inform($pad); ++$doneblank;
  4261.     }
  4262.     }
  4263.     return $nums;
  4264. }
  4265.  
  4266. sub describe_pkg {
  4267.     my $o = shift;
  4268.     my $pkg = shift;
  4269.     my ($extra_keys, $extra_vals) = (shift || [], shift || []);
  4270.     my $n; 
  4271.  
  4272.     # Get the PPM::PPD object out of the PPM::Package object.
  4273.     my $pkg_des = $pkg->describe($o->conf('target'))->result;
  4274.  
  4275.     # Basic information:
  4276.     $n = $o->print_pairs(
  4277.     [qw(Name Version Author Title Abstract), @$extra_keys],
  4278.     [(map { $pkg_des->$_ } qw(name version author title abstract)),
  4279.      @$extra_vals],
  4280.     undef,    # separator
  4281.     undef,    # left
  4282.     undef,    # indent
  4283.     undef,    # length
  4284.     1,    # wrap (yes, please wrap)
  4285.     );
  4286.  
  4287.     # The repository:
  4288.     if (my $rep = $pkg_des->repository) {
  4289.     $o->print_pairs(
  4290.         ["Location"],
  4291.         [$rep->name],
  4292.         undef,    # separator
  4293.         undef,    # left
  4294.         undef,    # indent
  4295.         $n,        # length
  4296.         1,        # wrap
  4297.     );
  4298.     }
  4299.     
  4300.     # Prerequisites:
  4301.     my @impls = grep { $_->architecture } $pkg_des->implementations;
  4302.     my @prereqs = @impls ? $impls[0]->prereqs : ();
  4303.     $o->inform("Prerequisites:\n") if @prereqs;
  4304.     $o->print_pairs(
  4305.     [ 1 .. @prereqs ],
  4306.     [ map { $_->name . ' ' . $_->version} @prereqs ],
  4307.     '. ',    # separator
  4308.     undef,    # left
  4309.     undef,    # indent
  4310.     $n,    # length
  4311.     0,    # wrap (no, please don't wrap)
  4312.     );
  4313.     
  4314.     # Implementations:
  4315.     $o->inform("Available Platforms:\n") if @impls;
  4316.     my @impl_strings;
  4317.     for (@impls) {
  4318.     my $arch  = $_->architecture;
  4319.     my $os    = $_->os;
  4320.     my $osver = $_->osversion;
  4321.     my $str   = $arch;
  4322.     $osver    =~ s/\Q(any version)\E//g;
  4323.     if ($os and $osver) {
  4324.         $str .= ", $os $osver";
  4325.     }
  4326.     push @impl_strings, $str;
  4327.     }
  4328.     @impl_strings = dictsort @impl_strings;
  4329.     $o->print_pairs(
  4330.     [ 1 .. @impls ],
  4331.     [ @impl_strings ],
  4332.     '. ', undef, undef, $n
  4333.     );
  4334. }
  4335.  
  4336. sub remove_pkg {
  4337.     my $o = shift;
  4338.     my $package = shift;
  4339.     my $target = $o->conf('target');
  4340.     my $force = shift;
  4341.     my $quell_clear = shift;
  4342.     my $verbose = $o->conf('remove-verbose');
  4343.     my $ok = PPM::UI::remove($target, $package, $force, sub { $o->cb_remove(@_) }, $verbose);
  4344.     unless ($ok->is_success) {
  4345.     $o->warn($ok->msg);
  4346.     return 0 unless $ok->ok;
  4347.     }
  4348.     else {
  4349.     $o->warn_profile_change($ok);
  4350.     }
  4351.     $o->cache_clear('query') if ($ok->ok and not $quell_clear);
  4352.     1;
  4353. }
  4354.  
  4355. sub upgrade_pkg {
  4356.     push @_, 'upgrade';
  4357.     goto &install_pkg;
  4358. }
  4359. sub install_pkg {
  4360.     my $o = shift;
  4361.     my $pkg = shift;
  4362.     my $opts = shift;
  4363.     my $action = shift;
  4364.     my $quell_clear = shift;
  4365.     $action = 'install' unless defined $action;
  4366.  
  4367.     # Find the package:
  4368.     while (1) {
  4369.     # 1. Return if they specified a full filename or URL:
  4370.     last if PPM::UI::is_pkg($pkg);
  4371.  
  4372.     # 2. Check if whatever they specified returns 1 search result:
  4373.     my $search =
  4374.       PPM::UI::search([$o->reps_on], $o->conf('target'), $pkg, 
  4375.               $o->conf('case-sensitivity'));
  4376.     unless ($search->is_success) {
  4377.         $o->warn($search->msg);
  4378.         return unless $search->ok;
  4379.     }
  4380.     my @ret = $search->result_l;
  4381.     if (@ret > 1) {
  4382.         $o->warn(<<END);
  4383. Searching for '$pkg' returned multiple results. Using 'search' instead...
  4384. END
  4385.         $o->run_search($pkg);
  4386.         return;
  4387.     }
  4388.     elsif (not @ret) {
  4389.         $o->warn(<<END);
  4390. Searching for '$pkg' returned no results. Try a broader search first.
  4391. END
  4392.         return;
  4393.     }
  4394.     $pkg = $ret[0]->name;
  4395.     last;
  4396.     }
  4397.  
  4398.     my $cb = (
  4399.     $opts->{dryrun}
  4400.     ? $action eq 'install' ? \&dr_install : \&dr_upgrade
  4401.     : $action eq 'install' ? \&cb_install : \&cb_upgrade
  4402.     );
  4403.  
  4404.     # Now, do the install
  4405.     my $ok;
  4406.     my @rlist = $o->reps_on;
  4407.     my $targ = $o->conf('target');
  4408.  
  4409.     if ($action eq 'install') {
  4410.     $opts->{verbose} = $o->conf('install-verbose');
  4411.     my $prop = PPM::UI::properties($targ, $pkg);
  4412.     my $pkgname = ref $pkg ? eval { $pkg->name } || $pkg : $pkg;
  4413.     $o->inform("Note: Package '$pkgname' is already installed.\n")
  4414.         if $prop->ok;
  4415.     $ok = PPM::UI::install(\@rlist, $targ, $pkg, $opts, sub {$o->$cb(@_)});
  4416.     }
  4417.     else {
  4418.     $opts->{verbose} = $o->conf('upgrade-verbose');
  4419.     $ok = PPM::UI::upgrade(\@rlist, $targ, $pkg, $opts, sub {$o->$cb(@_)});
  4420.     }
  4421.  
  4422.     unless ($ok->is_success) {
  4423.     $o->warn($ok->msg);
  4424.     return unless $ok->ok;
  4425.     }
  4426.     else {
  4427.     $o->warn_profile_change($ok);
  4428.     $o->cache_clear('query') unless $quell_clear;
  4429.     }
  4430.     1;
  4431. }
  4432.  
  4433. # The dry run callback; just prints out package name and version:
  4434. sub dr_install {
  4435.     my $o = shift;
  4436.     my $pkg = shift;
  4437.     my $version = shift;
  4438.     my $target_name = shift;
  4439.     $o->inform(<<END);
  4440. Dry run install '$pkg' version $version in $target_name.
  4441. END
  4442. }
  4443.  
  4444. sub dr_upgrade {
  4445.     my $o = shift;
  4446.     my $pkg = shift;
  4447.     my $version = shift;
  4448.     my $target_name = shift;
  4449.     $o->inform(<<END);
  4450. Dry run upgrade '$pkg' version $version in $target_name.
  4451. END
  4452. }
  4453.  
  4454. sub dr_remove {
  4455.     my $o = shift;
  4456.     my $pkg = shift;
  4457.     my $version = shift;
  4458.     my $target_name = shift;
  4459.     $o->inform(<<END);
  4460. Dry run remove '$pkg' version $version from $target_name.
  4461. END
  4462. }
  4463.  
  4464. sub cb_remove {
  4465.     my $o = shift;
  4466.     my $pkg = shift;
  4467.     my $version = shift;
  4468.     my $target_name = shift;
  4469.     my $status = shift;
  4470.     if ($status eq 'COMPLETE') {
  4471.     $o->inform(
  4472.         "Successfully removed $pkg version $version from $target_name.\n"
  4473.     )
  4474.     }
  4475.     else {
  4476.     $o->inform(<<END);
  4477. $SEP
  4478. Remove '$pkg' version $version from $target_name.
  4479. $SEP
  4480. END
  4481.     }
  4482. }
  4483.  
  4484. sub cb_install {
  4485.     my $o = shift;
  4486.     unshift @_, $o, 'install';
  4487.     &cb_status;
  4488. }
  4489.  
  4490. sub cb_upgrade {
  4491.     my $o = shift;
  4492.     unshift @_, $o, 'upgrade';
  4493.     &cb_status;
  4494. }
  4495.  
  4496. sub cb_status {
  4497.     my $o = shift;
  4498.     my $ACTION = shift;
  4499.     my $pkg = shift;
  4500.     my $version = shift;
  4501.     my $target_name = shift;
  4502.     my $status = shift;
  4503.     my $bytes = shift;
  4504.     my $total = shift;
  4505.     my $secs = shift;
  4506.  
  4507.     my $cols = $ENV{COLUMNS} || 78;
  4508.  
  4509.     $o->inform(<<END) and return if ($status eq 'PRE-INSTALL');
  4510. $SEP
  4511. \u$ACTION '$pkg' version $version in $target_name.
  4512. $SEP
  4513. END
  4514.  
  4515.     # Print the output on one line, repeatedly:
  4516.     my ($line, $pad, $eol);
  4517.     if ($status eq 'DOWNLOAD') {
  4518.     if ($bytes < $total) {
  4519.         $line = "Transferring data: $bytes/$total bytes.";
  4520.         $eol = "\r";
  4521.     }
  4522.     else {
  4523.         $line = "Downloaded $bytes bytes.";
  4524.         $eol = "\n";
  4525.     }
  4526.     }
  4527.     elsif ($status eq 'PRE-EXPAND') {
  4528.     $line = ""; #"Extracting package. This may take a few seconds.";
  4529.     $eol = "\r";  #"\n";
  4530.     }
  4531.     elsif ($status eq 'EXPAND') {
  4532.     $line = "Extracting $bytes/$total: $secs";
  4533.     $eol = $bytes < $total ? "\r" : "\n";
  4534.     }
  4535.     elsif ($status eq 'COMPLETE') {
  4536.     my $verb = $ACTION eq 'install' ? 'installed' : 'upgraded';
  4537.     $o->inform(
  4538.         "Successfully $verb $pkg version $version in $target_name.\n"
  4539.     );
  4540.     return;
  4541.     }
  4542.     $pad = ' ' x ($cols - length($line));
  4543.     $o->verbose($line, $pad, $eol);
  4544. }
  4545.  
  4546. sub warn_profile_change {
  4547.     my $o = shift;
  4548.     my $ok = shift;
  4549.  
  4550.     my $profile_track = $o->conf('profile-track');
  4551.     my $profile = PPM::UI::profile_get()->result;
  4552.  
  4553.     if ($profile_track) {
  4554.     $o->verbose(<<END);
  4555. Tracking changes to profile '$profile'.
  4556. END
  4557.     }
  4558. }
  4559.  
  4560. sub parse_range {
  4561.     my @numbers;
  4562.     my $arg;
  4563.     while ($arg = shift) {
  4564.       while ($arg) {
  4565.     if ($arg =~ s/^\s*,?\s*(\d+)\s*-\s*(\d+)//) {
  4566.         push @numbers, ($1 .. $2);
  4567.     }
  4568.     elsif ($arg =~ s/^\s*,?\s*(\d+)//) {
  4569.         push @numbers, $1;
  4570.     }
  4571.     else {
  4572.         last;
  4573.     }
  4574.       }
  4575.     }
  4576.     @numbers;
  4577. }
  4578.  
  4579. sub raw_args {
  4580.     my $o = shift;
  4581.     strip($o->line_args);
  4582. }
  4583.  
  4584. sub strip {
  4585.     my $f = shift;
  4586.     $f =~ s/^\s*//;
  4587.     $f =~ s/\s*$//;
  4588.     $f;
  4589. }
  4590.  
  4591. # matches("neil", "ne|il") => 1
  4592. # matches("ne", "ne|il") => 1
  4593. # matches("n", "ne|il") => 0
  4594. sub matches {
  4595.     my $cmd = shift;
  4596.     my $pat = shift || "";
  4597.  
  4598.     my ($required, $extra) = split '\|', $pat;
  4599.     $extra ||= "";
  4600.     my $regex = "$required(?:";
  4601.     for (my $i=1; $i<=length($extra); $i++) {
  4602.     $regex .= '|' . substr($extra, 0, $i);
  4603.     }
  4604.     $regex .= ")";
  4605.     return $cmd =~ /^$regex$/i;
  4606. }
  4607.  
  4608. sub pause_exit {
  4609.     my $o = shift;
  4610.     my $exit_code = shift || 0;
  4611.     my $pause = shift || 0;
  4612.     if ($pause) {
  4613.     if ($o->have_readkey) {
  4614.         $o->inform("Hit any key to exit...");
  4615.     }
  4616.     else {
  4617.         $o->inform("Hit <ENTER> to exit...");
  4618.     }
  4619.     $o->readkey;
  4620.     }
  4621.     exit $exit_code;
  4622. }
  4623.  
  4624. #============================================================================
  4625. # Check if this is the first time we've ever used profiles. This can be
  4626. # guessed: if the 'profile' entry is not set, but the 'profile-track' flag
  4627. # is, then it's the first time profile-track has been set to '1'.
  4628. #============================================================================
  4629. sub setup_profile {
  4630.     my $o = shift;
  4631.     $o->inform(<<END);
  4632. $SEP
  4633. You have profile tracking turned on: now it's time to choose a profile name.
  4634. ActiveState's PPM 3.0 Server will track which packages you have installed on
  4635. your machine. This information is stored in a "profile", located on the
  4636. server.
  4637.  
  4638. Here are some features of profiles:
  4639.  o You can have as many profiles as you want;
  4640.  o Each profile can track an unlimited number of packages;
  4641.  o PPM defaults to "tracking" your profile (it updates your profile every time
  4642.    you add or remove a package;
  4643.  o You can disable profile tracking by modifying the 'profile-track' option;
  4644.  o You can manually select, save, and restore profiles;
  4645.  o You can view your profile from ASPN as well as inside PPM 3.
  4646. $SEP
  4647.  
  4648. END
  4649.  
  4650.     my $response = PPM::UI::profile_list();
  4651.     my @l;
  4652.     unless ($response->ok) {
  4653.     $o->warn($response->msg);
  4654.     $o->warn(<<END);
  4655.  
  4656. You can still use PPM3, but profiles are not enabled. To try setting up
  4657. profiles again, enter 'set profile-track=1'. Or, you can set up profiles
  4658. by hand, using the 'profile add' command.
  4659.  
  4660. END
  4661.     $o->run('unset', 'profile-track');
  4662.     return;
  4663.     }
  4664.     else {
  4665.     @l = sort $response->result_l;
  4666.     $o->inform("It looks like you have profiles on the server already.\n")
  4667.       if @l;
  4668.     $o->print_pairs([1 .. @l], \@l, '. ', 1, ' ');
  4669.     $o->inform("\n") if @l;
  4670.     }
  4671.  
  4672.     require PPM::Sysinfo;
  4673.     (my $suggest = PPM::Sysinfo::hostname()) =~ s/\..*$//;
  4674.     $suggest ||= "Default Profile";
  4675.     my $profile_name = $o->prompt(
  4676.     "What profile name would you like? [$suggest] ", $suggest, @l
  4677.     );
  4678.  
  4679.     my $select_existing = grep { $profile_name eq $_ } $response->result_l
  4680.       if $response->ok;
  4681.     if ($select_existing) {
  4682.     $o->inform("Selecting profile '$profile_name'...\n");
  4683.     PPM::UI::profile_set($profile_name);
  4684.     $o->inform(<<END);
  4685. You should probably run either 'profile save' or 'profile restore' to bring
  4686. the profile in sync with your computer.
  4687. END
  4688.     }
  4689.     elsif ($response->ok) {
  4690.     $o->inform("Creating profile '$profile_name'...\n");
  4691.     $o->run('profile', 'add', $profile_name);
  4692.     $o->inform("Saving profile '$profile_name'...\n");
  4693.     $o->run('profile', 'save');
  4694.     $o->inform(<<END);
  4695. Congratulations! PPM is now set up to track your profile.
  4696. END
  4697.     }
  4698.     else {
  4699.     $o->warn($response->msg);
  4700.     $o->warn(<<END);
  4701.  
  4702. You can still use PPM3, but profiles will not be enabled. To try setting up
  4703. profiles again, enter 'set profile-track=1'. Or, you can set up profiles
  4704. yourself using the 'profile add' command.
  4705.  
  4706. END
  4707.     $o->run('unset', 'profile-track');
  4708.     }
  4709. }
  4710.  
  4711. package main;
  4712. use Getopt::Long;
  4713. use Data::Dumper;
  4714.  
  4715. $ENV{PERL_READLINE_NOWARN} = "1";
  4716. $ENV{PERL_RL} = $^O eq 'MSWin32' ? "0" : "Perl";
  4717.  
  4718. my ($pause, $input_file, $target);
  4719.  
  4720. BEGIN {
  4721.     my ($shared_config_files, @fixpath, $gen_inst_key);
  4722.  
  4723.     Getopt::Long::Configure('pass_through');
  4724.     $target = 'auto';
  4725.     GetOptions(
  4726.     'file=s' => \$input_file,
  4727.     'shared' => \$shared_config_files,
  4728.     'target:s' => \$target,
  4729.     'fixpath=s' => \@fixpath,
  4730.     'generate-inst-key' => \$gen_inst_key,
  4731.     pause => \$pause,
  4732.     );
  4733.     Getopt::Long::Configure('no_pass_through');
  4734.  
  4735.     if ($shared_config_files) {
  4736.     $ENV{PPM3_shared_config} = 1;
  4737.     }
  4738.  
  4739.     if (@fixpath) {
  4740.     PPM::UI::target_fix_paths(@fixpath);
  4741.     exit;
  4742.     }
  4743.     if ($gen_inst_key) {
  4744.     require PPM::Config;
  4745.     PPM::Config::load_config_file('instkey');
  4746.     exit;
  4747.     }
  4748. }
  4749.  
  4750. # If we're being run from a file, tell Term::Shell about it:
  4751. if ($input_file) {
  4752.     my $line = 0;
  4753.     open SCRIPT, $input_file or die "$0: can't open $input_file: $!";
  4754.     my $shell = PPMShell->new(
  4755.     term => ['PPM3', \*SCRIPT, \*STDOUT],
  4756.     target => $target,
  4757.     pager => 'none',
  4758.     );
  4759.     $shell->setmode('SCRIPT');
  4760.     while (<SCRIPT>) {
  4761.     $line++;
  4762.     next if /^\s*#/ or /^\s*$/;
  4763.     my ($cmd, @args) = $shell->line_parsed($_);
  4764.     my $ret = $shell->run($cmd, @args);
  4765.     my $warn = <<END;
  4766. $0: $input_file:$line: fatal error: unknown or ambiguous command '$cmd'. 
  4767. END
  4768.     $shell->warn($warn) and $shell->pause_exit(2, $pause)
  4769.         unless $shell->{API}{cmd}{run}{found};
  4770.     $shell->pause_exit(1, $pause) unless $ret;
  4771.     }
  4772.     close SCRIPT;
  4773.     $shell->pause_exit(0, $pause);
  4774. }
  4775.  
  4776. # If we've been told what to do from the command-line, do it right away:
  4777. elsif (@ARGV) {
  4778.     my $shell = PPMShell->new(target => $target, pager => 'none');
  4779.     $shell->setmode('BATCH');
  4780.     my $ret = $shell->run($ARGV[0], @ARGV[1..$#ARGV]);
  4781.     my $warn = <<END;
  4782. Unknown or ambiguous command '$ARGV[0]'; type 'help' for commands.
  4783. END
  4784.     $shell->warn($warn) and $shell->pause_exit(2, $pause)
  4785.     unless $shell->{API}{cmd}{run}{found};
  4786.     $shell->pause_exit(0, $pause) if $ret;
  4787.     $shell->pause_exit(1, $pause);
  4788. }
  4789.  
  4790. # Just run the command loop
  4791. if (-t STDIN and -t STDOUT) {
  4792.     my $shell = PPMShell->new(target => $target);
  4793.     $shell->setmode('SHELL');
  4794.     $shell->cmdloop;
  4795. }
  4796. else {
  4797.     die <<END;
  4798.  
  4799. Error:
  4800.     PPM3 cannot be run in interactive shell mode unless both STDIN and
  4801.     STDOUT are connected to a terminal or console. If you want to
  4802.     capture the output of a command, use PPM3 in batch mode like this:
  4803.  
  4804.        ppm3 search IO-stringy > results.txt
  4805.  
  4806.     Type 'perldoc ppm3' for more information.
  4807.  
  4808. END
  4809. }
  4810.  
  4811.  
  4812. =head1 NAME
  4813.  
  4814. ppm3-bin - ppm3 executable
  4815.  
  4816. =head1 SYNOPSIS
  4817.  
  4818. Do not run I<ppm3-bin> manually. It is meant to be called by the wrapper
  4819. program I<ppm3>. See L<ppm3>.
  4820.  
  4821. =head1 DESCRIPTION
  4822.  
  4823. I<ppm3> runs I<ppm3-bin> after setting up a few environment variables. You
  4824. should run I<ppm3> instead.
  4825.  
  4826. For information about I<ppm3> commands, see L<ppm3>.
  4827.  
  4828. =head1 SEE ALSO
  4829.  
  4830. See L<ppm3>.
  4831.  
  4832. =head1 AUTHOR
  4833.  
  4834. ActiveState Corporation (support@ActiveState.com)
  4835.  
  4836. =head1 COPYRIGHT
  4837.  
  4838. Copyright (C) 2001, 2002, ActiveState Corporation. All Rights Reserved.
  4839.  
  4840. =cut
  4841.  
  4842. __END__
  4843. :endofperl
  4844.